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;
}
LightOJ1283 Shelving Books(DP)的更多相关文章
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)
.navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- 最长公共子序列长度(dp)
/// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...
随机推荐
- Retrofit学习入门
Retrofit的使用 设置权限与添加依赖 定义请求接口 通过创建一个retrofit生成一个接口的实现类(动态代理) 调用接口请求数据 设置权限与添加依赖 权限:首先确保在AndroidManife ...
- Jmeter 提取http请求返回值里json数据参数化方法
第三方插件下载地址:http://jmeter-plugins.org/downloads/all/ 插件下载后解压:找到JMeterPlugins-Extras.jar,把JMeterPlugins ...
- highcharts的简单使用
在使用过的图表js插件中,个人认为还是highcharts最好,无论从兼容性,渲染速度,甚至是文档详细上来说,都一直觉得highcharts更胜一筹.现在花点时间做一下简单的总结,比如从一个矩形图开始 ...
- Web框架本质
Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:utf- ...
- HTML5应用之文件拖拽上传
使用HTML5的文件API,可以将操作系统中的文件拖放到浏览器的指定区域,实现文件上传到服务器.本文将结合实例讲解HTML5+jQuery+PHP实现拖拽上传图片的过程,来看下HTML5的魅力吧. H ...
- tkprof工具详解二(一些实例)
TKPROF是一个可执行文件,自带在Oracle Server软件中,无需额外的安装. 该工具文件可以用来解析ORACLE的SQL TRACE(10046) 以便生成更可读的内容. 实际上tkpro ...
- 解决oracle11g 空表不能exp导出的问题
在使用exp备份数据库,然后使用imp导入的时候出现了好多表或者视图不存在的错误信息. 究其原因,是11G中增加了一个新的特性:数据条数是0时不分配segment,所以就不能被导出. 解决思路:就是向 ...
- cmder
添加cmder到右键菜单 Cmder.exe /REGISTER ALL 打开配置快捷键 win+alt+p 文字重叠 main->font->去掉monospace的勾 λ符号修改 找到 ...
- VS2015 Preview Secondary Installer 离线安装
VS2015 Preview Secondary Installer 离线安装 天朝的原因orz, 装过vs2015 preview 的人都懂的,第二阶段安装会失败.假公济私的研究了下VS2015,摸 ...
- java中如何实现类似goto的作法
goto虽然是java中保留的keyword,但是对于跳转这个语法对新手来说这个确实好用.为了提高程序的可靠性和可读性,Java语言目前是不支持无条件跳转的goto语句!! 幸亏java中有高仿跳转的 ...