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. 使用idea的的第一个坑-----javax.xml.ws.WebServiceRef

    新建项目启动报错的时候,一直报这个错,类找不到,郁闷了半天,都没百度到结果,后来发现是添加tomcat的时候jre没 指定..... 哈哈哈,太懵逼了,指定就ok了

  2. Linux MySQl 5.7.17 MySQL ERROR 1366(HY000):Incorrect string value 解决方法

    MySQL ERROR 1366(HY000):Incorrect string value,在往数据库中插入中文的时候会出现. 这也就是编码问题,网上大部分都是说设置下配置文件中的设置,而可悲的是在 ...

  3. MySQL参数设置

    InnoDB配置 从MySQL 5.5版本开始,InnoDB就是默认的存储引擎并且它比任何其它存储引擎的使用要多得多.那也是为什么它需要小心配置的原因. 1 innodb_file_per_table ...

  4. 25 The Go image/draw package go图片/描绘包:图片/描绘包的基本原理

    The Go image/draw package  go图片/描绘包:图片/描绘包的基本原理 29 September 2011 Introduction Package image/draw de ...

  5. kafka集群及监控部署

    1. kafka的定义 kafka是一个分布式消息系统,由linkedin使用scala编写,用作LinkedIn的活动流(Activity Stream)和运营数据处理管道(Pipeline)的基础 ...

  6. 关于SQLserver的索引的一些脚本

    --判断无用的索引: SELECT TOP 30 DB_NAME() AS DatabaseName , '[' + SCHEMA_NAME(o.Schema_ID) + ']' + '.' + '[ ...

  7. MySQL学习笔记:从一个表update到另外一个表

    # ---- 测试数据 ---- # 表1 CREATE TABLE temp_x AS AS c_id, 1.11 AS c_amount FROM DUAL UNION ALL AS c_id, ...

  8. 20155225 2016-2017-2 《Java程序设计》第十周学习总结

    20155225 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 22章网络 22.1网络概览 22.2超文本传输协议(HTTP) 22.2.1 HTTP请求 ...

  9. 20165333 2017-2018-2《Java程序设计》课程总结

    一.每周作业链接汇总 1.预备作业一:我期望的师生关系 简要内容: 印象深刻的老师 我期望的师生关系 关于JAVA学习 2.预备作业二:学习基础和C语言学习基础 简要内容: 技能学习 C语言学习 关于 ...

  10. Java模拟按键

    JDK自带了Robot类,此类用于为测试自动化.自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件.Robot 的主要目的是便于 Java 平台实现自动测试. 详情可查看jdk1.6 ...