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. KDE下安装fcitx后终端不能输入中文

    编辑用户的  ~/.profile 文件(或/etc/profile): #fcitx export XIM_PROGRAM=fcitx export XIM=fcitx export GTK_IM_ ...

  2. Genymotion上不能安装APK软件的问题

    Genymotion模拟器不能安装APK的原因 官网给出的解释:Genymotion模拟器使用的是x86架构,在第三方市场上的应用有部分不采用x86这么一种架构,所以在编译的时候不通过,报“APP n ...

  3. pip安装遇到问题

    安装pip之后,在cmd下输入 pip --version始终提示: Unknown option:versionDid not provide a command自己安装步骤没错,怎么想也不明白,无 ...

  4. MongoDB-MongoDB重装系统后恢复

    重装系统后,把原mongoDB安装目录和原mongoDB的data目录拷贝到新硬盘的D盘上. 恢复的方法如下. 1.D:\Mongodb里放着mongod.cfg和data C:\Users\Admi ...

  5. java基础78 Servlet的生命周期

    1.Servlet的生命周期 简单的解析就是: 创建servlet实例(调用构造器)---->调用init()方法---->调用service()方法----->调用destroy( ...

  6. 简单的UDP接受程序

    //功能:客服端发送UDP包,服务器接受到并打印出来//2015.9.13成功 #include <stdio.h>#include <sys/socket.h>#includ ...

  7. elasticsearch文档学习

    1.集群 节点(一个elasticsearch实体)  索引  主节点 :集群级别变更,新增或移除节点,索引:  主节点不参与文档级别搜索和变更. 分片(shard):一个完整的搜索引擎,lucene ...

  8. Sql Server 添加、更新、查询表注释、字段注释相关sql

    /*******************字段添加注释*********************/ if not exists (SELECT C.value AS column_description ...

  9. gluster学习(一)

    2)Bricks • Brick是一个节点和一个导出目录的集合,e.g. node1:/brick1 • Brick是底层的RAID或磁盘经XFS或ext4文件系统格式化而来,所以继承了文件系统的限制 ...

  10. 7-1 FireTruck 消防车 uva208

    题意: 输入一个n <=20 个结点的无向图以及某个结点k   按照字典序从小到大顺序输出从结点1到结点k的所有路径  要求结点不能重复经过 标准回溯法 要实现从小到大字典序 现在数组中排序好即 ...