【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 = 3005;

int n, a[N], sum[N], dp[N*3], pre[N * 3];

int main() {
    while(~scanf("%d", &n)) {
        sum[0] = 0;
        for(int i = 1; i <= n; i ++) {
            scanf("%d", &a[i]);
            if(a[i] > 0 && a[i] <= n)    sum[i] = sum[i - 1] + 1;
            else    sum[i] = sum[i - 1];
        }
        memset(dp, 0, sizeof(dp));
        memset(pre, 0, sizeof(pre));
        int ma = 0, ans = 0;
        for(int i = n; i >= 1; i --) {
            for(int c = 1 - a[i]; c <= n - a[i]; c ++)
                ma = max(ma, ++ dp[c + n]);
            for(int c = 1 - n; c <= 2 * n; c ++) {
                ans = max(ans, sum[i - 1] + dp[c + n]);
                ans = max(ans, sum[i - 1] + dp[c + n] + pre[c + n]);
            }
            for(int c = 1 - n; c <= 2 * n; c ++)
                pre[c + n] = max(pre[c + n], ma - dp[c + n]);
        }
        printf("%d\n", ans);
    }
}

猜你喜欢

转载自www.cnblogs.com/wans-caesar-02111007/p/9826413.html
今日推荐