日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LightOJ1283 Shelving Books(DP)

發布時間:2023/12/13 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的LightOJ1283 Shelving Books(DP)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。