SPOJ || POJ - 2228 Naptime【环型DP】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niiick/article/details/81873923

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.


题目分析

首先假设一天的第N小时与后一天的第一个小时不相连
这种情况下DP转移比较好想

d p [ i ] [ j ] [ 0 / 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][0],dp[i-1][j-1][1]+a[i]);

初始化为 d p [ 1 ] [ 0 ] [ 0 ] = d p [ 1 ] [ 1 ] [ 1 ] = 0
其余为 i n f
答案为 m a x ( d p [ n ] [ b ] [ 0 ] , d p [ n ] [ b ] [ 1 ] )

现在再考虑一天的第N小时与后一天的第一个小时相连
我们发现上述转移中
唯一没考虑到的情况只有第1个小时休息能获得体力

于是我们可以初始化 d p [ 1 ] [ 1 ] [ 1 ] = U 1
转移方程与上述相同
那么答案为 d p [ n ] [ b ] [ 1 ] (即强制最后一小时休息令第一小时能获得体力)
和前一次dp的答案比较即可得到最终结果

到此为止再SPOJ上已经可以AC
但是!!!POJ上的Memory limit只有丧心病狂的64M
于是我们考虑用滚动数组优化空间

dp[i&1][j][0]=max(dp[(i-1)&1][j][0],dp[(i-1)&1][j][1]);
dp[i&1][j][1]=max(dp[(i-1)&1][j-1][0],dp[(i-1)&1][j-1][1]+a[i]);

因为dp[i][][]只与dp[i-1][][]有关
所以只要交替使用数组第0维和第1维
只保存上一次更新的dp数组
即可大幅优化空间


#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
typedef long long lt;

int read()
{
    int x=0,f=1;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return x*f;
}

const int maxn=4010;
int t,n,m,ans;
int a[maxn];
int dp1[2][maxn][2],dp2[2][maxn][2];

int main()
{
    t=read();
    while(t--)
    {
        n=read();m=read();ans=0;
        for(int i=1;i<=n;++i)a[i]=read();

        memset(dp1,128,sizeof(dp1));
        dp1[1][0][0]=dp1[1][1][1]=0;

        memset(dp2,128,sizeof(dp2));
        dp2[1][1][1]=a[1];

        for(int i=2;i<=n;++i)
        for(int j=0;j<=min(i,m);++j)
        {
            dp1[i&1][j][0]=max(dp1[(i-1)&1][j][0],dp1[(i-1)&1][j][1]);
            if(j>=1)dp1[i&1][j][1]=max(dp1[(i-1)&1][j-1][0],dp1[(i-1)&1][j-1][1]+a[i]);

            dp2[i&1][j][0]=max(dp2[(i-1)&1][j][0],dp2[(i-1)&1][j][1]);
            if(j>=1)dp2[i&1][j][1]=max(dp2[(i-1)&1][j-1][0],dp2[(i-1)&1][j-1][1]+a[i]);
        }
        ans=max(dp2[n&1][m][1],max(dp1[n&1][m][1],dp1[n&1][m][0]));
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/81873923
今日推荐