3622 假期(DP+单调队列优化)

来源:互联网 发布:mac壁纸 2560x1440 编辑:程序博客网 时间:2024/06/08 08:45

3622 假期
时间限制: 1 s
空间限制: 64000 KB
题目等级 : 黄金 Gold
题目描述 Description
经过几个月辛勤的工作,FJ决定让奶牛放假。假期可以在1…N天内任意选择一段(需要连续),每一天都有一个享受指数W。但是奶牛的要求非常苛刻,假期不能短于P天,否则奶牛不能得到足够的休息;假期也不能超过Q天,否则奶牛会玩的腻烦。FJ想知道奶牛们能获得的最大享受指数。
输入描述 Input Description
第一行:N,P,Q.
第二行:N个数字,中间用一个空格隔开。
输出描述 Output Description
一个整数,奶牛们能获得的最大享受指数。
样例输入 Sample Input
5 2 4
-9 -4 -3 8 -6
样例输出 Sample Output
5
数据范围及提示 Data Size & Hint
50% 1≤N≤10000
100% 1≤N≤100000
1<=p<=q<=n
指数在longint范围之内。
提示:
选择第3-4天,享受指数为-3+8=5。

/*DP+单调队列.处理一个前缀和sum. 要求ans=sum[i]-sum[k](i-Q<=k<=i-P).要使ans最大.则sum[k]最小.然后在区间[i-Q,i-P]中单调队列维护一个min就阔以了. */#include<iostream>#include<cstdio>#define MAXN 100001#define LL long longusing namespace std;LL sum[MAXN],n,k,l,x,ans=-1e18,head,tail,q[MAXN],P,Q;LL read(){    LL x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();    return x*f;}int main(){    n=read();P=read(),Q=read();    for(int i=1;i<=n;i++) sum[i]=read(),sum[i]+=sum[i-1];    for(int i=P;i<=n;i++)    {        while(head<tail&&sum[i-P]<sum[q[tail-1]])            tail--;        q[tail++]=i-P;        while(head<tail&&i-q[head]>Q)            head++;        ans=max(ans,sum[i]-sum[q[head]]);    }    printf("%lld",ans);    return 0;}
0 0