列表

详情


NC51186. Naptime

描述

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

示例1

输入:

5 3
2
0
3
1
4

输出:

6

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 24ms, 内存消耗: 528K, 提交时间: 2022-01-16 11:39:37

#include <bits/stdc++.h>
using namespace std;
int n,b,u[3835],dp[2][3835][2];
void DP(){
	for(int i=2;i<=n;i++)for(int j=0;j<=i;j++){
        dp[i&1][j][0]=max(dp[(i-1)&1][j][0],dp[(i-1)&1][j][1]);
        if(j>0)dp[i&1][j][1]=max(dp[(i-1)&1][j-1][0],dp[(i-1)&1][j-1][1]+u[i]);
    }
}
int main(){
    while(cin>>n>>b){
        for(int i=1;i<=n;i++)cin>>u[i];
        if(!b){cout<<"0\n";continue;}
        memset(dp,-0x3f3f3f3f,sizeof(dp));
        dp[1][0][0]=0;
		dp[1][1][1]=0;
        DP();
        int ans=max(dp[n&1][b][0],dp[n&1][b][1]);
        memset(dp,-0x3f3f3f3f,sizeof(dp));
        dp[1][1][1]=u[1];
        DP();
        ans=max(ans,dp[n&1][b][1]);
		cout<<ans<<"\n";
    }
    return 0;
}

上一题