题目描述
Naptime
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7651 Accepted: 3078 Description
Goneril is a very sleep-deprived cow. Her day is partitioned into N (3 <= N <= 3,830) equal time periods but she can spend only B (2 <= B < N) not necessarily contiguous periods in bed. Due to her bovine hormone levels, each period has its own utility U_i (0 <= U_i <= 200,000), which is the amount of rest derived from sleeping during that period. These utility values are fixed and are independent of what Goneril chooses to do, including when she decides to be in bed.
With the help of her alarm clock, she can choose exactly which periods to spend in bed and which periods to spend doing more critical items such as writing papers or watching baseball. However, she can only get in or out of bed on the boundaries of a period.
She wants to choose her sleeping periods to maximize the sum of the utilities over the periods during which she is in bed. Unfortunately, every time she climbs in bed, she has to spend the first period falling asleep and gets no sleep utility from that period.
The periods wrap around in a circle; if Goneril spends both periods N and 1 in bed, then she does get sleep utility out of period 1.
What is the maximum total sleep utility Goneril can achieve?Input
* Line 1: Two space-separated integers: N and B
* Lines 2..N+1: Line i+1 contains a single integer, U_i, between 0 and 200,000 inclusiveOutput
The day is divided into 5 periods, with utilities 2, 0, 3, 1, 4 in that order. Goneril must pick 3 periods.
Sample Input
5 3 2 0 3 1 4Sample Output
6Hint
INPUT DETAILS:
The day is divided into 5 periods, with utilities 2, 0, 3, 1, 4 in that order. Goneril must pick 3 periods.
OUTPUT DETAILS:
Goneril can get total utility 6 by being in bed during periods 4, 5, and 1, with utilities 0 [getting to sleep], 4, and 2 respectively.
题目大意:
一天由 n 个小时构成,在第 i 个小时睡觉能够恢复 点体力。有一头牛要休息 b 个小时,可以不连续,但休息的第 1 个小时无法恢复体力。前一天的最后一个小时和第二天的第一个小时是连在一起的,求这头牛能恢复的体力最大值。
思路
这道题比较明显往DP方面想。关键在于这句话:
前一天的最后一个小时和第二天的第一个小时是连在一起的
这句话让我们必须考虑跨越最后一个小时和第一个小时的情况,一下就增加了难度。一开始想把数组扩展成2n来DP,但这样其实不和题意,因为这道题并不是一个循环的问题。
这样一个小的条件让我们不好写出一个状态转移方程,那我们就分类讨论:我们会发现,如果最后一个小时不休息,那和不连起来是一样的,正常DP就行;如果最后一个小时要休息,那我们只需修改一下DP数组的初值,默认最后一个小时要休息。这样做两次DP,取最优解即可。
状态转移方程如下:
(DP[i][j][]的前两维表示前 i 小时,休息 j 小时,最后一维代表第i小时休不休息。)
最后记得DP数组第一维要循环使用,否则会MLE。
AC代码
#include<iostream>
using std::cin;
using std::cout;
using std::max;
using std::endl;
int U[3831], dp1[3][3831][2];
int main(){
int N, B;
int res;
const int REST = 1, UNREST = 0;
cin>>N>>B;
for(int i = 1; i <= N; i++){
cin>>U[i];
}
for(int j = 0 ; j <= B; j++)
dp1[1][j][REST] = dp1[1][j][UNREST] = dp1[2][j][REST] = dp1[2][j][UNREST] = -0x3f3f3f3f;
dp1[2][1][REST] = dp1[2][0][UNREST] = 0;
for(int i = 2; i <= N; i++){
for(int j = 0; j <= B && j <= N; j++){
dp1[i % 2 + 1][j][UNREST] = max(dp1[(i - 1) % 2 + 1][j][REST], dp1[(i - 1) % 2 + 1][j][UNREST]);
if(j){
dp1[i % 2 + 1][j][REST] = max(dp1[(i - 1) % 2 + 1][j - 1][REST] + U[i], dp1[(i - 1) % 2 + 1][j - 1][UNREST]);
}
}
}
res = max(dp1[N % 2 + 1][B][REST], dp1[N % 2 + 1][B][UNREST]);
for(int j = 0 ; j <= B ; j++)
dp1[1][j][REST] = dp1[1][j][UNREST] = dp1[2][j][REST] = dp1[2][j][UNREST] = -0x3f3f3f3f;
dp1[2][1][REST] = U[1];
for(int i = 2; i <= N; i++){
for(int j = 0; j <= B && j <= N; j++){
dp1[i % 2 + 1][j][UNREST] = max(dp1[(i - 1) % 2 + 1][j][REST], dp1[(i - 1) % 2 + 1][j][UNREST]);
if(j){
dp1[i % 2 + 1][j][REST] = max(dp1[(i - 1) % 2 + 1][j - 1][REST] + U[i], dp1[(i - 1) % 2 + 1][j - 1][UNREST]);
}
}
}
cout<<max(res, dp1[N % 2 + 1][B][REST])<<endl;
return 0;
}