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.pswp.cn/news/254687.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/254687.shtml
英文地址,請注明出處:http://en.pswp.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…