Processing math: 100%

【POJ2228】Naptime

题目链接

Naptime

题目描述

Goneril is a very sleep-deprived cow. Her day is partitioned into N (3N3,830) equal time periods but she can spend only B (2B<N) not necessarily contiguous periods in bed. Due to her bovine hormone levels, each period has its own utility Ui (0Ui200,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?

输入格式

  • Line 1: Two space-separated integers: N and B
  • Lines 2...N+1: Line i+1 contains a single integer, Ui, between 0 and 200,000 inclusive

输出格式

The day is divided into 5 periods, with utilities 2,0,3,1,4 in that order. Goneril must pick 3 periods.

样例输入

5 3
2
0
3
1
4

样例输出

6

提示

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.

题解

有一个环,你可以选择一些区间,所有区间总共包含m个数。
求每个区间除去第一个数后剩下的数之和。
假设这道题的数是一条链而不是环,那么我们很容易想到直接dp就能做了。
那么我们考虑怎么处理环,这道题暴力断开每个点肯定是会T的,
那么我们考虑环和链的区别。
环和链相比就是多了一种区间跨越第n个数到第1个数后的情况。
那么我们可以分类讨论是否有区间跨越第n个数。
没有的话直接dp,
如果有,那么我们强制第1个数不是区间的第1个数,也就是第一个数对答案有贡献。
最后对答案取max的时候只选最后一个数在区间里的dp就好了。
上代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string.h>
using namespace std;
int n,b;
int a[4009];
int dp[2][4009][2];
int ans;
int main(){
    scanf("%d%d",&n,&b);
    for(int j=1;j<=n;j++)
        scanf("%d",&a[j]);
    memset(dp,-1,sizeof(dp));
    dp[1][1][1]=dp[1][0][0]=0;
    for(int j=2;j<=n;j++){
        for(int i=0;i<=j;i++){
            dp[j&1][i][1]=-1;
            dp[j&1][i][0]=-1;
            if(dp[(j-1)&1][i][0]!=-1) dp[j&1][i][0]=max(dp[j&1][i][0],dp[(j-1)&1][i][0]);
            if(dp[(j-1)&1][i][1]!=-1) dp[j&1][i][0]=max(dp[j&1][i][0],dp[(j-1)&1][i][1]);
            if(i==0) continue;
            if(dp[(j-1)&1][i-1][0]!=-1) dp[j&1][i][1]=max(dp[j&1][i][1],dp[(j-1)&1][i-1][0]);
            if(dp[(j-1)&1][i-1][1]!=-1) dp[j&1][i][1]=max(dp[j&1][i][1],dp[(j-1)&1][i-1][1]+a[j]);
        }
    }
    ans=max(dp[n&1][b][0],dp[n&1][b][1]);
    memset(dp,-1,sizeof(dp));
    dp[1][1][1]=a[1];
    for(int j=2;j<=n;j++){
        for(int i=0;i<=j;i++){
            dp[j&1][i][1]=-1;
            dp[j&1][i][0]=-1;
            if(dp[(j-1)&1][i][0]!=-1) dp[j&1][i][0]=max(dp[j&1][i][0],dp[(j-1)&1][i][0]);
            if(dp[(j-1)&1][i][1]!=-1) dp[j&1][i][0]=max(dp[j&1][i][0],dp[(j-1)&1][i][1]);
            if(i==0) continue;
            if(dp[(j-1)&1][i-1][0]!=-1) dp[j&1][i][1]=max(dp[j&1][i][1],dp[(j-1)&1][i-1][0]);
            if(dp[(j-1)&1][i-1][1]!=-1) dp[j&1][i][1]=max(dp[j&1][i][1],dp[(j-1)&1][i-1][1]+a[j]);
        }
    }
    ans=max(ans,dp[n&1][b][1]);
    printf("%d\n",ans);
    return 0;
}
posted @   oblivionl  阅读(163)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
阅读排行:
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· 在线客服系统 QPS 突破 240/秒,连接数突破 4000,日请求数接近1000万次,.NET 多
· C# 开发工具Visual Studio 介绍
· 在 Windows 10 上实现免密码 SSH 登录
· C#中如何使用异步编程
点击右上角即可分享
微信分享提示