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】的更多相关文章

  1. 【剑指Offer学习】【面试题66:矩阵中的路径】

    题目:请设计一个函数,用来推断在一个矩阵中是否存在一条包括某字符串全部字符的路径.路径能够从矩阵中随意一格開始.每一步能够在矩阵中间向左.右.上.下移动一格.假设一条路径经过了矩阵的某一格,那么该路径 ...

  2. 【剑指Offer学习】【面试题65:滑动窗体的最大值】

    题目:给定一个数组和滑动窗体的大小,请找出全部滑动窗体里的最大值. 举例说明 比如,假设输入数组{2,3,4,2,6,2,5,1}及滑动窗体的大小.那么一共存在6个滑动窗体,它们的最大值分别为{4,4 ...

  3. 【洛谷】3953:逛公园【反向最短路】【记忆化搜索(DP)统计方案】

    P3953 逛公园 题目描述 策策同学特别喜欢逛公园.公园可以看成一张N个点M条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,N号点是公园的出口,每条边有一个非负权值, 代表策策经过这条 ...

  4. 【Xamarin挖墙脚系列:常用的Mac 命令】

    通俗点说Mac 跟Linux的爹都是Unix,他们都加入了标准的Shell命令工具,bash 所以俩系统中的命令基本通用 Linux下的操作手册,本人自己整理了一份.呵呵~~~~ 还可以使用客户端远程 ...

  5. 【POJ】2796:Feel Good【单调栈】

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18449   Accepted: 5125 Case T ...

  6. 【Xamarin 挖墙脚系列:Xamarin SDK开源了................】

                                                                  在前不久举行的 Build 2016 开发者大会上,微软宣布它收购的 Xam ...

  7. 【WCF系列二:如何调用WCF服务】WCF入门教程(图文)VS2012

    上一遍到现在已经有一段时间了,先向关注本文的各位“挨踢”同仁们道歉了.小生自认为一个ITer如果想要做的更好,就需要将自己的所学.所用积极分享出来,接收大家的指导和吐槽.网上也有很多WCF相关的教程, ...

  8. 面向对象_05【类的继承:extends、重写父类】

    类的继承:现有类的基础上构建一个新的类,构建出来的类被称作子类,子类可继承父类的属性和方法. 什么时候定义继承?当类与类之间存在着所属关系的时候,就定义继承.xxx是yyy中的一种==>xxx ...

  9. 【python041--构造方法:构造和析造】

    一.魔法方法 1.魔法方法总是被双下划线包围,例如:__init__ 2.为什么在类实例化的时候,有时候需要构造__init__,有时候不需要呢 举例说明: #定义一个矩形的类,需要长和宽两个参数,计 ...

随机推荐

  1. 20165230 2017-2018-2 《Java程序设计》第6周学习总结

    20165230 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容总结 第八章 常用使用类 String类常用方法 public int length() publ ...

  2. perl6 HTTP::UserAgent发送post

    use HTTP::UserAgent; my $ua = HTTP::UserAgent.new; say 'All method:'; say $ua.^methods; my %data = : ...

  3. VirtualBox上安装CentOS-7(Minimal)

    Windows 10家庭中文版,VirtualBox 5.2.12,CentOS 7(Minimal版), 因为听到大家在谈论CentOS,阿里云上也有CentOS,CentOS还是Red Hat出品 ...

  4. C# byte[] 转换16进制字符串

    1.byte[] 转换16进制字符串 1.1 BitConverter方式 var str = DateTime.Now.ToString(); var encode = Encoding.UTF8; ...

  5. Python_oldboy_自动化运维之路_全栈考试(五)

    1.执行 Python 脚本的两种方式 [root@localhost tmp]# cat a.py #!/usr/bin/python # -*- coding: UTF-8 -*- print & ...

  6. 如何查看页面是否开启了gzip压缩

    1.谷歌浏览器 F12 2.在表头单击鼠标右键 3.如果开启了gzip则显示gzip,没有则是空

  7. javaweb 要学习的东西

    我学院课设是Javaweb程序,要用eclipse,tomcat,jbdc,和数据库 jbdc,是连接数据库的驱动,tomcat是一种类似于服务器的东西,现在买不起服务器,就用tomcat

  8. (转)链接服务器——获取EXCEL数据

    测试目的:验证利用链接服务器.分布式查询获取EXCEL中的数据测试环境:Microsoft SQL Server 2005 - 9.00.3080.00 (X64)  Enterprise Editi ...

  9. abtest分流随机链接方法(javascript)

    ¶¹¸¯¸ÉËêµÄ·¨¹úµçÊÓ¸²¸Ç --> 代码如下 <!DOCTYPE HTML> <html> <head> <script type=& ...

  10. python 判断字符编码

    一般情况下,需要加这个: import sys reload(sys) sys.setdefaultencoding('utf-8') 打开其他文件编码用codecs.open 读 下面的代码读取了文 ...