POJ - 2228 Naptime 有环DP 二次DP法

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 inclusive

Output

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
4

题意:一天有n小时,需要休息b小时,求恢复的体力最大值,每次休息第一个小时不恢复体力

做法:

dp[i][j][0]表示前i个数取j个数并且第i个数不取的最大体力

dp[i[j][1]表示前i个数取j个数并且第i个数取的最大体力

当不是环时

显然状态转移方程为dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1]);

dp[i][j][1]=max(dp[i-1][j-1][1]+a[i],dp[i-1][j-1][0];  答案为max(dp[n][m][1],dp[n][m][0]);

当是环时

发现只影响a[1]要加体力的情况,a[1]要加,第n天必须就要休息

初始化dp[1][1][1]=a[1],答案就是dp[n][m][1]

取二次最优答案即可   注意一下要用滚动数组优化

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int len=4e3+10;
const double pi=acos(-1.0);
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
int n,m;
int a[len];
int dp[2][len][2];
int main()
{  
	cin>>n>>m;
	for(int i=1;i<=n;++i)cin>>a[i];
	memset(dp,0,sizeof(dp));
	for(int i=2;i<=n;++i)
		for(int j=0;j<=m;++j)
		{
			dp[i&1][j][0]=dp[i&1][j][1]=0;
			dp[i&1][j][0]=max(dp[(i-1)&1][j][0],dp[(i-1)&1][j][1]);
			if(j>=1)dp[i&1][j][1]=dp[(i-1)&1][j-1][0];
			if(j>=2)dp[i&1][j][1]=max(dp[i&1][j][1],dp[(i-1)&1][j-1][1]+a[i]);
		}
	int ans=max(dp[n&1][m][1],dp[n&1][m][0]);
	memset(dp,0,sizeof(dp));
	dp[1][1][1]=a[1];
	for(int i=2;i<=n;++i)
		for(int j=0;j<=m;++j)
		{
			dp[i&1][j][0]=dp[i&1][j][1]=0;
			dp[i&1][j][0]=max(dp[(i-1)&1][j][0],dp[(i-1)&1][j][1]);
			if(j>=1)dp[i&1][j][1]=dp[(i-1)&1][j-1][0];
			if(j>=2)dp[i&1][j][1]=max(dp[i&1][j][1],dp[(i-1)&1][j-1][1]+a[i]);//注意j-1一定要大于1
		}
	ans=max(ans,dp[n&1][m][1]);
	cout<<ans<<endl;
}
		

猜你喜欢

转载自blog.csdn.net/hutwuguangrong/article/details/89342372