【ZOJ】3740:Water Level【DP】
Water Level
Time Limit: 2 Seconds Memory Limit: 65536 KB
Hangzhou is a beautiful city, especially the West Lake. Recently, the water level of the West Lake got lower and lower because of the hot weather. The experts think that the West Lake is under good situation if the water level is in a certain range. To maintain the good condition of the West Lake, we get at most 2 chances to pour or draw water.
So the question is:
There are N periods, each period the predicted water level of the West Lake is Ai(Ai could be under 0). Now, you can pour water C unit at period X. (if c<0 then it means drawing water.)Then the water level from period X to period N will be increase C(if c<0 then it means the water level will reduce |C|). But you have at most 2 chances.(Do nothing is OK!)
The government wants you to figure out the best plan that could make the periods that the water level is between 1 and N as many as possible.
Input
There are multiple test cases. For each test case:
The first line only contains one integer N(1<=N<=3000),N is the number of periods.
The second line contains N integers, the i-th integer Ai(-N<=Ai<=N) is the height of the water level in i-th period.
Process to the end of input.
Output
One line for each test case. The maximal number of periods that could make the water level in good condition.
Sample Input
6
2 -1 -1 5 -1 2
Sample Output
5
Hint
Pouring 2 unit water at Period 1, then(2,-1,-1,5,-1,2) -> (4,1,1,7,1,4). You get 5 periods (except 4-th) fit the demand.
If you use second chance to draw 1 unit water at Period 4, then(4,1,1,7,1,4) -> (4,1,1,6,0,3). You still get 5 periods (except 5-th) fit the demand.
Author: TANG, Yajie
Contest: ZOJ Monthly, December 2013
Solution
一开始看到这道题毫无思路啊QAQ
区间修改什么的很想线段树??
然而起点和修改的值都无法直接确定啊QAQ
所以最简单的做法是DP
首先处理出不修改就可以满足条件的前缀和,然后定义$dp[i]$表示当前点到最后修改$i$能够得到的符合条件的数量,所以显然从后往前更好维护。
而修改两次能得到的最大符合条件的数量就是$sum[i-1]+dp[c1,i]-dp[c1,j]+dp[c1+c2,j]$,c1,c2分别是第一次和第二次修改的值。
所以我们要求的是$sum[i-1]+dp[c1,i]+max(dp[c1+c2,j]-dp[c1,j])$,而$dp[c1,i]$可以在倒推过程中更新,后面的max可以对于每一个$c1$处理出来,每次找出最大的$dp[c1+c2,j]$即可。
Code
#include<bits/stdc++.h>
using namespace std; const int N = ; int n, a[N], sum[N], dp[N*], pre[N * ]; int main() {
while(~scanf("%d", &n)) {
sum[] = ;
for(int i = ; i <= n; i ++) {
scanf("%d", &a[i]);
if(a[i] > && a[i] <= n) sum[i] = sum[i - ] + ;
else sum[i] = sum[i - ];
}
memset(dp, , sizeof(dp));
memset(pre, , sizeof(pre));
int ma = , ans = ;
for(int i = n; i >= ; i --) {
for(int c = - a[i]; c <= n - a[i]; c ++)
ma = max(ma, ++ dp[c + n]);
for(int c = - n; c <= * n; c ++) {
ans = max(ans, sum[i - ] + dp[c + n]);
ans = max(ans, sum[i - ] + dp[c + n] + pre[c + n]);
}
for(int c = - n; c <= * n; c ++)
pre[c + n] = max(pre[c + n], ma - dp[c + n]);
}
printf("%d\n", ans);
}
}
【ZOJ】3740:Water Level【DP】的更多相关文章
- 【剑指Offer学习】【面试题66:矩阵中的路径】
题目:请设计一个函数,用来推断在一个矩阵中是否存在一条包括某字符串全部字符的路径.路径能够从矩阵中随意一格開始.每一步能够在矩阵中间向左.右.上.下移动一格.假设一条路径经过了矩阵的某一格,那么该路径 ...
- 【剑指Offer学习】【面试题65:滑动窗体的最大值】
题目:给定一个数组和滑动窗体的大小,请找出全部滑动窗体里的最大值. 举例说明 比如,假设输入数组{2,3,4,2,6,2,5,1}及滑动窗体的大小.那么一共存在6个滑动窗体,它们的最大值分别为{4,4 ...
- 【洛谷】3953:逛公园【反向最短路】【记忆化搜索(DP)统计方案】
P3953 逛公园 题目描述 策策同学特别喜欢逛公园.公园可以看成一张N个点M条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,N号点是公园的出口,每条边有一个非负权值, 代表策策经过这条 ...
- 【Xamarin挖墙脚系列:常用的Mac 命令】
通俗点说Mac 跟Linux的爹都是Unix,他们都加入了标准的Shell命令工具,bash 所以俩系统中的命令基本通用 Linux下的操作手册,本人自己整理了一份.呵呵~~~~ 还可以使用客户端远程 ...
- 【POJ】2796:Feel Good【单调栈】
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 18449 Accepted: 5125 Case T ...
- 【Xamarin 挖墙脚系列:Xamarin SDK开源了................】
在前不久举行的 Build 2016 开发者大会上,微软宣布它收购的 Xam ...
- 【WCF系列二:如何调用WCF服务】WCF入门教程(图文)VS2012
上一遍到现在已经有一段时间了,先向关注本文的各位“挨踢”同仁们道歉了.小生自认为一个ITer如果想要做的更好,就需要将自己的所学.所用积极分享出来,接收大家的指导和吐槽.网上也有很多WCF相关的教程, ...
- 面向对象_05【类的继承:extends、重写父类】
类的继承:现有类的基础上构建一个新的类,构建出来的类被称作子类,子类可继承父类的属性和方法. 什么时候定义继承?当类与类之间存在着所属关系的时候,就定义继承.xxx是yyy中的一种==>xxx ...
- 【python041--构造方法:构造和析造】
一.魔法方法 1.魔法方法总是被双下划线包围,例如:__init__ 2.为什么在类实例化的时候,有时候需要构造__init__,有时候不需要呢 举例说明: #定义一个矩形的类,需要长和宽两个参数,计 ...
随机推荐
- 【洛谷题解】P2303 [SDOi2012]Longge的问题
题目传送门:链接. 能自己推出正确的式子的感觉真的很好! 题意简述: 求\(\sum_{i=1}^{n}gcd(i,n)\).\(n\leq 2^{32}\). 题解: 我们开始化简式子: \(\su ...
- React-Native 之 环境配置和简单使用
# 前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会 ...
- Android调试大法 自定义IDE默认签名文件==>微信支付、微信登录、微信分享,debug时调试通过,release时调不起微信
转载地址:http://blog.yanzhenjie.com Android调试大法之自定义IDE默认签名文件,你是否为调试第三方SDK时debug签名和release签名发生冲突而烦恼?你是否在d ...
- github后端开发面试题大集合(二)
作者:小海胆链接:https://www.nowcoder.com/discuss/3615?type=0&order=0&pos=8&page=0来源:牛客网 7.非关系型数 ...
- SQL数据是否存在(是否有数据)判断,表,存储过程是否存在
判断是否存在数据 if exists( select * from Hong_PageConfig where names='name' ) Begin print '1' End else Begi ...
- GreenPlum学习笔记:create table创建表
二维表同样是GP中重要的存储数据对象,为了更好的支持数据仓库海量数据的访问,GP的表可以分成: 面向行存储的普通堆积表 面向列存储的AOT表(append only table) 当然AOT表也可以是 ...
- C11多线程
参考: http://www.oschina.net/translate/cplusplus-11-threading-make-your-multitasking-life http://blog. ...
- (转)最短路算法--Dijkstra算法
转自:http://blog.51cto.com/ahalei/1387799 上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短 ...
- hdoj1863 畅通工程(Prime || Kruskal)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1863 思路 最小生成树问题,使用Prime算法或者Kruskal算法解决.这题在hdoj1233的基础 ...
- C#导出HTML到PDF组件Pechkin
http://www.knowsky.com/898441.html C#导出PDF功能是开发中经常遇到的功能,我们采用第三方的组件,比如 iTextSharp, aspose等,还能搜到一些开源的类 ...