LightOJ1283 Shelving Books(DP)

题目

Source

http://www.lightoj.com/volume_showproblem.php?problem=1283

Description

You are a librarian. You keep the books in a well organized form such that it becomes simpler for you to find a book and even they look better in the shelves.

One day you get n new books from one of the library sponsors. And unfortunately they are coming to visit the library, and of course they want to see their books in the shelves. So, you don't have enough time to shelve them all in the shelf in an organized manner since the heights of the books may not be same. But it's the question of your reputation, that's why you have planned to shelve them using the following idea:

1) You will take one book from the n books from left.
2) You have exactly one shelf to organize these books, so you may either put this book in the left side of the shelf, right side of the shelf or you may not put it in the shelf. There can already be books in the left or right side. In such case, you put the book with that book, but you don't move the book you put previously.
3) Your target is to put the books in the shelf such that from left to right they are sorted in non-descending order.
4) Your target is to put as many books in the shelf as you can.

You can assume that the shelf is wide enough to contain all the books. For example, you have 5 books and the heights of the books are 3 9 1 5 8 (from left). In the shelf you can put at most 4 books. You can shelve 3 5 8 9, because at first you got the book with height 3, you stored it in the left side of the shelf, then you got 9 and you put it in the right side of the shelf, then you got 1 and you ignored it, you got 5 you put it in the left with 3. Then you got 5 and you put it in left or right. You can also shelve 1 5 8 9 maintaining the restrictions.

Now given the heights of the books, your task is to find the maximum number of books you can shelve maintaining the above restrictions.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 100). Next line contains n space separated integers from [1, 105]. The ith integer denotes the height of the ith book from left.

Output

For each case, print the case number and the maximum number of books that can be shelved.

Sample Input

2
5
3 9 1 5 8
8
121 710 312 611 599 400 689 611

Sample Output

Case 1: 4
Case 2: 6

 

分析

题目大概说有n本书,要依次把它们放到书架,可以放到书架的左边或者右边挨着已经放好的书的下一个位置,当然也可以选择不放。放好后要保证书的高度从左到右非递减。问最多能放上几本书。

 

n才100,果断这么表示状态:

  • dp[i][j][k]表示放置前i本书,书架的左边最后面的书是第j本且书架右边最前面的书是第k本,最多能放的书数

转移我用我为人人,通过dp[i-1]的状态选择将第i本书放到左边还是右边或者不放来转移并更新dp[i]的状态值。

 

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int d[111][111][111];
int main(){int t,n,a[111];scanf("%d",&t);for(int cse=1; cse<=t; ++cse){scanf("%d",&n);for(int i=1; i<=n; ++i){scanf("%d",&a[i]);}memset(d,-1,sizeof(d));d[0][0][0]=0;for(int i=0; i<n; ++i){for(int j=0; j<=i; ++j){for(int k=0; k<=i; ++k){if(d[i][j][k]==-1) continue;d[i+1][j][k]=max(d[i+1][j][k],d[i][j][k]);if(j==0 && k==0){d[i+1][i+1][0]=1;d[i+1][0][i+1]=1;}else if(j==0){if(a[k]>=a[i+1]) d[i+1][i+1][k]=max(d[i+1][i+1][k],d[i][j][k]+1);if(a[k]>=a[i+1]) d[i+1][j][i+1]=max(d[i+1][j][i+1],d[i][j][k]+1);}else if(k==0){if(a[i+1]>=a[j]) d[i+1][i+1][k]=max(d[i+1][i+1][k],d[i][j][k]+1);if(a[i+1]>=a[j]) d[i+1][j][i+1]=max(d[i+1][j][i+1],d[i][j][k]+1);}else{if(a[j]<=a[i+1] && a[i+1]<=a[k]){d[i+1][i+1][k]=max(d[i+1][i+1][k],d[i][j][k]+1);d[i+1][j][i+1]=max(d[i+1][j][i+1],d[i][j][k]+1);}}}}}int res=0;for(int i=0; i<=n; ++i){for(int j=0; j<=n; ++j){res=max(res,d[n][i][j]);}}printf("Case %d: %d\n",cse,res);}return 0;
}

 

转载于:https://www.cnblogs.com/WABoss/p/5765310.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/254687.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

量子传输技术转移一个人需要4500万亿年

看过《星际迷航》的朋友一定不会忘记这句经典的台词&#xff1a;斯科蒂&#xff0c;将我传输过去&#xff01;其中涉及到量子隐形传输的技术&#xff0c;可以把物体从三维时空一处传输到另一处。但可惜的是&#xff0c;这种看着非常炫的技术或许根本无法实现。 到目前为止&…

使用OutputDebugString帮助调试

使用OutputDebugString帮助调试 前面我已经介绍了使用TRACE来帮助我们调试&#xff0c;但使用TRACE有一个限制&#xff0c;只能在将程序DEBUG编译状态下才能使用&#xff0c;下面我们介绍OutputDebugString函数&#xff0c;通过它&#xff0c;可以在在DEBUG或RELEASE情况也可以…

leetcode-551-Student Attendance Record I(判断是否出现连续几个相同字符)

题目描述&#xff1a; You are given a string representing an attendance record for a student. The record only contains the following three characters: A : Absent.L : Late.P : Present.A student could be rewarded if his attendance record doesnt contain more t…

简单实现

1.创建接口和实现类 &#xff08;模拟实现查询天气&#xff09; 接口&#xff1a; package com.learning.weather;/*** * weather 接口 &#xff1a;实现模拟wsdl*/ public interface WeatherInterface {/*** 查询天气* param name* return*/public String queryWeather(Strin…

halcon联合C#测量十字Mark中心

halcon联合C#测量十字Mark中心 函数说明 public void FitRectangleMeasure(HWindow 窗口句柄, HImage 图像, out double 中心Y坐标, out double 中心X坐标)操作步骤&#xff0c;首先绘制两个矩形测量框&#xff1b;之后就可进行自动计算。 public void FitRectangleMeasure(…

x264 struct 学习

x264_t结构体维护着CODEC的诸多重要信息其中成员frames是一个指示和控制帧编码过程的结构。其中current是已经准备就绪可以编码的帧&#xff0c;其类型已经确定&#xff1b;next是尚未确定类型的帧&#xff1b;unused用于回收不使用的frame结构体以备今后再次使用。 structx26…

单例模式的新实现

单例模式的新实现 jdk1.5 之前 单例模式的两种方式&#xff0c;两种方法都是要把构造器保持私有的&#xff0c;并导出公有的静态成员&#xff0c;以便允许客户端能够访问该类的唯一实例。 第一种方法中&#xff0c;公有的静态成员是个final域: //Singleton with public final f…

有关莫比乌斯反演

对于两个定义域为整数的函数F(x)和f(x); 若有: 然后F(x)可以快速求出&#xff1b; 如何用F求解f呢&#xff1f; 莫比乌斯反演&#xff1a; 对于两个定义域为整数的函数F(x)和f(x); 若有: 则有&#xff1a; 其中μ(x)为莫比乌斯函数&#xff0c;其定义为&#xff1a; 对于&#…

(原创)JS点击事件——Uncaught TypeError: Cannot set property 'onclick' of null

html部分代码&#xff1a; JS部分代码&#xff1a; 需要实现的效果&#xff1a;点击图片&#xff0c;来回相互切换。 我开始的错误做法&#xff1a;代码如上图所示&#xff08;逻辑上看起来是没有错误的&#xff09; 尝试过程&#xff1a;把JS代码放在</body>闭合标签之前…

事务传播机制/数据库异常解析——2016-8-13分享总结

一. 事务的传播机制&#xff0f;required 跟 required new 的使用与区别 基础回顾 1.1 事务的隔离级别&#xff1a; ISOLATION_READ_UNCOMMITTED&#xff08;读未提交&#xff09; ISOLATION_READ_COMMITTED&#xff08;读已提交&#xff09; ISOLATION_REPEATABLE_READ&#x…

console类详细解释

console类详细解释 微软链接https://docs.microsoft.com/zh-cn/dotnet/api/system.console?viewnetframework-4.8 C#中没有标准输入输出关键字&#xff0c;要调用console类下的方法。 练习与解释代码 using System; using System.Collections.Generic; using System.Linq; …

VC下调用x264进行视频编码,

4.X264.c中,h x264_encoder_open( param ) )是用来复制参数并验证参数的有效性,在CCS下应该是不需要验证参数的(参数都是在程序中设置好的),因此此处只作复制参数param和初始化X264_T h的操作.(VC下程序修改记录080106下午)修改COMMON.C中的void x264_param_default( x264_…

UploadRTOS.exe

UploadRTOS.exe类似于一个启动并为VxWin运行做准备的工具程序。VxWin安装之后&#xff0c;可以使用 上传工具程序 启动实时操作系统。 利用命令行参数,您可以使它执行不同的功能。该 上传工具程序 包含两个文件: UploadRTOS.exe (命令行程序) UploadR…

20155307 2016-2017 《Java程序设计》第三次实验报告

&#xff08;一&#xff09;敏捷开发与XP 敏捷开发是一种以人为核心、迭代、循序渐进的开发方法。“敏捷流程”是一系列价值观和方法论的集合。从2001年开始&#xff0c;一些软件界的专家开始倡导“敏捷”的价值观和流程&#xff0c;他们肯定了流行做法的价值&#xff0c;但是强…

ElasticSearch创建、修改、获取、删除、索引Indice mapping和Index Template案例

为什么80%的码农都做不了架构师&#xff1f;>>> The best elasticsearch highlevel java rest api-----bboss ElasticSearch客户端框架bboss的ClientInterface 接口提供了创建/修改、获取、删除索引Indice和IndexTemplate的方法&#xff0c;本文举例说明其使用方法…

ASCII码与string的相互转换

ASCII码与string的相互转换 思路&#xff1a; 1&#xff09;ASCII码转string&#xff1a;把字符&#xff08;串&#xff09;直接转换为int类型&#xff0c;即可得到ASCII码&#xff1b; 2&#xff09;string转ASCII码&#xff1a;将数字转换为字符串转出&#xff1b; {//将字…

X264代码中一些参数的意义

Main&#xff08;int argc&#xff0c;char *argv[]&#xff09;; 为了方便起见&#xff0c;不妨改写为&#xff1a; Main(void){ ...... intargc5; char*argv[]{ "main","-o","test.264","foreman.yuv","352x288" }; …

spring mvc注解@RequestMapping

1、url路径映射 基本功能 2、窄化请求映射 根路径子路径 注意setViewName的路径。 3、限制http请求方法 get和 post 如果是get 转载于:https://www.cnblogs.com/jway1101/p/5773923.html

Start application automatically during controller boot-up

&#xfeff;&#xfeff; Tip English •German Start application automatically during controller boot-up Description It is possible to start any program automatically during the boot-up procedure of the KR C4 controller. Precondition •User group “Exper…

C#using static

平常用法&#xff1a; using 命名空间&#xff1b; using System; Console.WriteLine("Hello&#xff0c;World&#xff01;");using static用法&#xff1a; C#6中支持这种写法&#xff0c;这样定义后可以可以访问类的静态成员 WriteLine是Console类的静态函数&am…