Naptime POJ - 2228(环形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
    Sample Output
    6
    Hint
    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.

思路:首相这个结构是环形的,环形有许多的处理方法,针对具体的题目而言,我们可以先把他变成一个线性dp,及从1到n的线性dp
状态定义:
F[i][[j][0] 表示前i个小时休息了j小时且第i小时不休息的获得能力的最大值。
F[i][[j][1] 表示前i个小时休息了j小时且第i小时休息的获得能力的最大值。
初值 F[1][1][0]=F[1][1][1]=0 ,其他均为负无穷(负无穷即为不合法)
状态转移方程:
F[i][[j][[0]=max(f[i1][j][0],f[i1][j][1])
F[i][[j][[1]=max(f[i1][j1][0],f[i1][j1][1]+Ui)
目标 max(F[n][b][0],F[n][b][1])
可以发现这种转移,从来没有考虑把 U1 纳入考虑范围
那么我们假设 U1 会作为最终答案的贡献,那么最终第n的时候,也必须休息所以
那么我们只要把初值 F[1][1][1]=U1 ,其他均为负无穷(包括 F[1][1][0] ,因为在这种设定下,这种状态是不合法的),再做一次dp.
目标 F[n][b][1] .
代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define maxx 4000
using namespace std;
long long dp[2][maxx][2];
int a[maxx];
int n,m;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    memset(dp,0xcf,sizeof(dp));
    dp[1][0][0]=dp[1][1][1]=0;
    for(int i=2;i<=n;i++)
    {
        for(int j=0;j<=m;j++)
        {
            dp[i&1][j][0]=max(dp[(i-1)&1][j][0],dp[(i-1)&1][j][1]);
            if(j)
                dp[i&1][j][1]=max(dp[(i-1)&1][j-1][0],dp[(i-1)&1][j-1][1]+a[i]);
        }
    }
    long long ans=max(dp[n&1][m][0],dp[n&1][m][1]);
    memset(dp,0xcf,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]=max(dp[(i-1)&1][j][0],dp[(i-1)&1][j][1]);
            if(j)
                dp[i&1][j][1]=max(dp[(i-1)&1][j-1][0],dp[(i-1)&1][j-1][1]+a[i]);
        }
    }
    ans=max(ans,dp[n&1][m][1]);
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值