码迷,mamicode.com
首页 > 其他好文 > 详细

[dp] zoj 3740 Water Level

时间:2014-05-10 10:12:56      阅读:432      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   tar   ext   

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5122

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
Submit    Status

题目意思:

给n个[-n,n]之间的数,在某一个位置i可以加任意的一个c,加了后a[i]-a[n]都会加c.最多可以加两次,求最终数值落在[1,n]之间的数的个数最多为多少。

解题思路:

dp预处理

dp[i]表示增加i后达到要求的数的个数,sum[i]表示从a[1]-a[i]不加时能满足的个数。

考虑加两次的情况第一次为c1,第二次为c2,ans=sum+dp[c1,i]-dp[c1,j]+dp[c2,j]=sum+dp[c1,i]+max(dp[c2,j]-dp[c1][j])

单独考虑max(dp[c2,j]-dp[c1,j]) =max(max(dp[c2,j])-dp[c1,j]) 对每个c1可以保存一个这个值,从后往前维护,记为la[c1]

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 3100

int dp[Maxn*3],la[Maxn*3],save[Maxn],fi[Maxn],n;


int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   while(~scanf("%d",&n))
   {
       fi[0]=0;
       for(int i=1;i<=n;i++)
       {
           scanf("%d",&save[i]);
           if(save[i]>=1&&save[i]<=n)
                fi[i]=fi[i-1]+1;
           else
                fi[i]=fi[i-1];
       }
       memset(dp,0,sizeof(dp));
       memset(la,0,sizeof(la));

       int Max=0,ans=0;

       for(int i=n;i>=1;i--)
       {
           for(int c=1-save[i];c<=n-save[i];c++)
                Max=max(Max,++dp[c+n]); //当前save[i]加了c后一定可以满足要求
           for(int c=1-n;c<=2*n;c++) //枚举所有可能加的值,注意可以是不是使得当前值满足要求的增值
           {
               ans=max(ans,fi[i-1]+dp[c+n]); //放一次
               ans=max(ans,fi[i-1]+dp[c+n]+la[c+n]); //放两次
           }
           for(int c=1-n;c<=2*n;c++)
                la[c+n]=max(la[c+n],Max-dp[c+n]);
       }
       printf("%d\n",ans);

   }
   return 0;
}


 


[dp] zoj 3740 Water Level,布布扣,bubuko.com

[dp] zoj 3740 Water Level

标签:style   blog   class   code   tar   ext   

原文地址:http://blog.csdn.net/cc_again/article/details/24989789

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!