Light OJ 1283 Shelving Books (区间DP)
来源:互联网 发布:prim和kruskal算法 编辑:程序博客网 时间:2024/06/14 01:28
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
Output for Sample Input
2
5
3 9 1 5 8
8
121 710 312 611 599 400 689 611
Case 1: 4
Case 2: 6
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>using namespace std;typedef long long ll;const int maxn= 150;const int INF = 0x3f3f3f3f;int t,n;int dp[maxn][maxn][maxn],a[maxn],s[maxn];int dfs(int p,int l,int r){ if(dp[p][l][r]!=-1)return dp[p][l][r]; if(p>n)return 0; int res = dfs(p+1,l,r); if(a[p]>=a[l] && a[p]<=a[r]){ res = max(res,max(dfs(p+1,p,r)+1,dfs(p+1,l,p)+1)); } dp[p][l][r]=res; return res;}int main(){ //freopen("123.txt","r",stdin); //freopen("out.txt","w",stdout); cin>>t; int ca = 0; while(t--){ cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; a[0]=0;a[n+1]=INF; memset(dp,-1,sizeof dp); ca++; cout<<"Case "<<ca<<": "<<dfs(1,0,n+1)<<endl; }}
- Light OJ 1283 Shelving Books (区间DP)
- Light OJ 1283 Shelving Books (区间DP)
- Light OJ 1283 Shelving Books (DP 分治)
- lightoj 1283 - Shelving Books DP
- lightoj 1283 1283 - Shelving Books(dp)
- Light oj 1025 (区间dp)
- light oj 1031(区间dp入门)
- light oj 1031(区间dp+博弈)
- light oj 1422(区间dp 水题)
- light oj 1422,区间dp
- light oj 1422(区间dp)
- light oj 1025 区间dp
- Light OJ 1031 区间dp
- 区间DP :Light OJ 1031
- light oj 1422 - Halloween Costumes (区间dp)
- light oj 1422 Halloween Costumes(区间dp)
- Light OJ 1422 Halloween Costumes(区间DP)
- light oj 1422 Halloween Costumes (区间DP)
- 如何把MYD,MYI文件导入到MYSQL数据库中?
- POJ 3186 Treats for the Cows (区间DP)
- Android之Inflate()方法用途
- Java泛型擦除(泛型反射)
- 《Linux学习》之文件目录
- Light OJ 1283 Shelving Books (区间DP)
- iOS中的屏幕适配之Masonry和SnapKit
- 网站、网页的自身优化--(如何让网站被搜索引擎搜索到)
- hdu_5616_Jam's balance(暴力枚举子集||母函数)
- jquery ajax向spring mvc controller中传值并接受及解析返回值
- 处理显式游标
- java基础/IO流/
- 汽水瓶问题
- PHP如何生成验证码