题目

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

LightOJ1283 Shelving Books(DP)相关推荐

  1. 求三角形最大面积(DP)

    求三角形最大面积(DP) 在OJ上奇迹般WA了:WA:70. Why? #include <iostream> #include <string.h> using namesp ...

  2. LeetCode 编辑距离 II(DP)

    1. 题目 给你两个单词 s 和 t,请你计算出将 s 转换成 t 所使用的最少操作数. 你可以对一个单词进行如下两种操作: 删除一个字符 替换一个字符 注意: 不允许插入操作 题目保证有解 示例: ...

  3. LeetCode 1220. 统计元音字母序列的数目(DP)

    文章目录 1. 题目 2. 解题 1. 题目 给你一个整数 n,请你帮忙统计一下我们可以按下述规则形成多少个长度为 n 的字符串: - 字符串中的每个字符都应当是小写元音字母('a', 'e', 'i ...

  4. LeetCode 265. 粉刷房子 II(DP)

    文章目录 1. 题目 2. 解题 1. 题目 假如有一排房子,共 n 个,每个房子可以被粉刷成 k 种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同. 当然,因为市场上不同颜色油 ...

  5. LeetCode 256. 粉刷房子(DP)

    文章目录 1. 题目 2. 解题 1. 题目 假如有一排房子,共 n 个,每个房子可以被粉刷成红色.蓝色或者绿色这三种颜色中的一种,你需要粉刷所有的房子并且使其与相邻的两个房子颜色不能相同. 当然,因 ...

  6. LeetCode 1223. 掷骰子模拟(DP)

    1. 题目 有一个骰子模拟器会每次投掷的时候生成一个 1 到 6 的随机数. 不过我们在使用它时有个约束,就是使得投掷骰子时,连续 掷出数字 i 的次数不能超过 rollMax[i](i 从 1 开始 ...

  7. LeetCode 1155. 掷骰子的N种方法(DP)

    1. 题目 这里有 d 个一样的骰子,每个骰子上都有 f 个面,分别标号为 1, 2, -, f. 我们约定:掷骰子的得到总点数为各骰子面朝上的数字的总和. 如果需要掷出的总点数为 target,请你 ...

  8. LeetCode 1139. 最大的以 1 为边界的正方形(DP)

    1. 题目 给你一个由若干 0 和 1 组成的二维网格 grid,请你找出边界全部由 1 组成的最大 正方形 子网格,并返回该子网格中的元素数量.如果不存在,则返回 0. 示例 1: 输入:grid ...

  9. 程序员面试金典 - 面试题 17.23. 最大黑方阵(DP)

    1. 题目 给定一个方阵,其中每个单元(像素)非黑即白. 设计一个算法,找出 4 条边皆为黑色像素的最大子方阵. 返回一个数组 [r, c, size] ,其中 r, c 分别代表子方阵左上角的行号和 ...

最新文章

  1. 安全获取QueryString的值类库下载
  2. oracle存储过程备份,利用ORACLE存储过程与JOB结合实现对数据表自动备份
  3. zabbix 搭建笔记
  4. react性能优化方案_React灵敏且性能卓越的Spray + Akka解决方案,以“在Java和Node.js中发挥并发性和性能”...
  5. Django框架架构总览
  6. 逻辑思维与C/C++解题
  7. 云端软件关闭的原因是什么?
  8. c语言编写墓碑上的字符,恶的大写字母是什么
  9. youtube下载利器
  10. as_completed函数用例
  11. c语言中怎样写入百分号,C语言中如何用printf函式输出百分号?
  12. 【风控策略】通过查全率和查准率确定cutoff
  13. 交换机级联,堆叠,集群技术介绍
  14. UNIX网络编程卷1 回射客户程序 TCP客户程序设计范式
  15. P2916 [USACO08NOV]Cheering up the Cow G 题解
  16. Eclipse中Java项目转化为Java Web项目
  17. PromQL 直方图 跟踪请求的延迟或响应大小 99%的请求是在多少延迟下完成的?
  18. linux中24点游戏下载,怀旧24点官网版-怀旧24点游戏下载v2.0.0-Linux公社
  19. 浏览器打开html不加载css样式,ie怎么加载不了css样式?
  20. Android lunch分析以及产品分支构建

热门文章

  1. python绘制散点图、如何选两列作为横坐标_Python利用matplotlib绘制散点图的新手教程...
  2. arcgis select by attributes一次选多个_地理工具学习--arcgis篇:单工具学习(2)
  3. 图神经网络(一)图信号处理与图卷积神经网络(1)矩阵乘法的三种方式
  4. java浮点数存储方式_Java浮点数内存存储
  5. arggis怎么修改上下标_京东自营是怎么操作的?有什么要求?
  6. 『操作系统』微内核结构的操作系统几何?(优缺点)
  7. 如何查看linux系统版本信息及CPU信息
  8. IP3 三阶交调截取点测试(转帖)
  9. RHEL/CentOS/Fedora各种源
  10. 卷积神经网络如何解释和预测图像