【bzoj1737】[Usaco2005 jan]Naptime 午睡时间 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?
输入
* 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
输出
* Line 1: A single integer, the maximum total sleep utility Goneril can achieve.
样例输入
5 3
2
0
3
1
4
样例输出
6
题解
dp
先不管环的问题,想象成一个时间段。
那么很容易想到状态转移方程:
f[i][j]=max(f[i-1][j-1]+w[i],g[i-1][j-1])
g[i][j]=max(f[i-1][j],g[i-1][j])
其中f[i][j]表示前i个小时中总共睡j个小时,且其i个小时睡的最大效用值,
g[i][j]表示前i个小时中总共睡j个小时,且其i个小时不睡的最大效用值。
答案就是max(f[n][b],g[n][b])。
然后考虑环的问题。
除了刚才讨论的情况之外,如果出现环,一定是从某个点开始,经过n和1,再停止。
这时候n和1一定是睡的情况。
考虑断环,那么和正常情况相比,唯一的区别就是从1开始的一段中,w[1]也算进了答案中(题目中描述:每一段的第一段都不算进效用值)。
所以改一下初始条件,再按照同样的方法跑一遍dp即可,答案是f[n][b]。
最后取最大值即可。
由于空间限制,需要使用滚动数组黑科技,看代码应该不难理解。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int f[2][3831] , g[2][3831] , w[3831];
int main()
{
int n , b , i , j , ans = 0x80808080;
scanf("%d%d" , &n , &b);
for(i = 1 ; i <= n ; i ++ )
scanf("%d" , &w[i]);
memset(f , 0x80 , sizeof(f));
memset(g , 0x80 , sizeof(g));
f[1][1] = g[1][0] = 0;
for(i = 2 ; i <= n ; i ++ )
{
for(j = 0 ; j <= b ; j ++ )
{
if(j)
f[i & 1][j] = max(f[(i & 1) ^ 1][j - 1] + w[i] , g[(i & 1) ^ 1][j - 1]);
g[i & 1][j] = max(f[(i & 1) ^ 1][j] , g[(i & 1) ^ 1][j]);
}
}
ans = max(f[n & 1][b] , g[n & 1][b]);
memset(f , 0x80 , sizeof(f));
memset(g , 0x80 , sizeof(g));
f[1][1] = w[1];
for(i = 2 ; i <= n ; i ++ )
{
for(j = 0 ; j <= b ; j ++ )
{
if(j)
f[i & 1][j] = max(f[(i & 1) ^ 1][j - 1] + w[i] , g[(i & 1) ^ 1][j - 1]);
g[i & 1][j] = max(f[(i & 1) ^ 1][j] , g[(i & 1) ^ 1][j]);
}
}
ans = max(ans , f[n & 1][b]);
printf("%d\n" , ans);
return 0;
}
【bzoj1737】[Usaco2005 jan]Naptime 午睡时间 dp的更多相关文章
- BZOJ1737 [Usaco2005 jan]Naptime 午睡时间
断环然后裸DP就好了... $f[i][j][k]$表示1号时间段没有被算入答案,到了第$i$个时间段,一共选了$j$个时间段,$k = 0 /1$表示第i个时间段有没有被算进答案的最优值 $g[i] ...
- BZOJ 1677: [Usaco2005 Jan]Sumsets 求和( dp )
完全背包.. --------------------------------------------------------------------------------------- #incl ...
- 1677: [Usaco2005 Jan]Sumsets 求和
1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 626 Solved: 348[Submi ...
- BZOJ1679: [Usaco2005 Jan]Moo Volume 牛的呼声
1679: [Usaco2005 Jan]Moo Volume 牛的呼声 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 723 Solved: 346[ ...
- BZOJ1677: [Usaco2005 Jan]Sumsets 求和
1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 570 Solved: 310[Submi ...
- BZOJ 1679: [Usaco2005 Jan]Moo Volume 牛的呼声( )
一开始直接 O( n² ) 暴力..结果就 A 了... USACO 数据是有多弱 = = 先sort , 然后自己再YY一下就能想出来...具体看code --------------------- ...
- BZOJ 1677: [Usaco2005 Jan]Sumsets 求和
题目 1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 617 Solved: 344[Su ...
- bzoj 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 最小点覆盖
链接 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 思路 这就是个上一篇的稍微麻烦版(是变脸版,其实没麻烦) 用边长为1的模板覆盖地图上的没有长草的土地,不能覆盖草地 ...
- 43 We were Born to Nap 我们天生需要午睡
We were Born to Nap 我们天生需要午睡 ①American society is not nap-friendly.In fact, says David Dinged, a sle ...
随机推荐
- JavaScript基础part1
JavaScript介绍 你不知道它是什么就学?这就是一个网页嵌入式脚本语言...仅此而已 JavaScript组成 一个完整的 JavaScript 实现是由以下 3 个不同部分组成的: 核心(EC ...
- nmap教程(下)
九.脚本引擎 脚本文件存放在/usr/share/nmap/scripts目录下 SCRIPT SCAN: -sC: equivalent to --script=default #启用默认类脚本 - ...
- 20145202马超 2016-2017-2 《Java程序设计》第11周学习总结
20145202马超 2016-2017-2 <Java程序设计>第11周学习总结 教材学习内容总结 XX 教材学习中的问题和解决过程 教材学习有问题先去https://shimo.im/ ...
- 北京Uber优步司机奖励政策(1月30日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 深圳Uber优步司机奖励政策(12月28日到1月3日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 每天看一片代码系列(三):codepen上一个音乐播放器的实现
今天我们看的是一个使用纯HTML+CSS+JS实现音乐播放器的例子,效果还是很赞的: codePen地址 HTML部分 首先我们要思考一下,一个播放器主要包含哪些元素.首先要有播放的进度信息,还有播放 ...
- 4456: [Zjoi2016]旅行者
4456: [Zjoi2016]旅行者 https://www.lydsy.com/JudgeOnline/problem.php?id=4456 分析: 每次对当前矩阵按长边化一条分治线,然后在对分 ...
- iOS SSL Pinning 保护你的 API
随着互联网的发展,网站全面 https 化已经越来越被重视,做为 App 开发人员,从一开始就让 API 都走 SSL 也是十分必要的.但是光这样就足够了吗? SSL 可以保护线上 API 数据不被篡 ...
- 获取App的PackageName包名和LauncherActivity启动页
第一种情况: 查看手机里面已经安装的App: 用数据线连接手机, 打开开发者模式, 并赋予相关权限: 1. 清除日志: adb logcat -c 2. 启动日志: adb logcat Activi ...
- APP九宫格滑动解锁的处理
写手机自动化测试脚本关于APP九宫格滑动解锁方面采用了appium API 之 TouchAction 操作. 先是用uiautomatorviewer.bat查询APP元素坐标: 手工计算九宫格每个 ...