Problem : [tju1071]一个简单题
Time Limit: 1 Sec Memory Limit: 128 MB
Description
有N个数字,从中选择出连续M(L1<=M<=L2)个数,求出他们之和的最大值
Input
第一排有三个数N, L1, L2。接下来的N行,每行一个数
<=L1<=L2<=N, 1<=N<=200000, -2*109<=每个数<=2*109
Output
一个数字,表示求出来的和的最大值
Sample Input
10 6 8
49
-87
-34
-97
61
33
-65
-63
56
50
Sample Output
72
代码如下
#include<cstdio>
#include<algorithm>
using namespace std;
int a[200010],sum[200010],dl[200010],maxx,i,n,l1,l2,head=1,tail;
int main() {
scanf("%d %d %d",&n,&l1,&l2);
for(i=1;i<=n;i++) {
scanf("%d",a+i);
sum[i]=sum[i-1]+a[i];
}
for(i=l1;i<=n;i++) {
while(head<=tail&&sum[i-l1]<=sum[dl[tail]]) tail--;
dl[++tail]=i-l1;
while(i-dl[head]>l2) head++;
maxx=max(maxx,sum[i]-sum[dl[head]]);
}
printf("%d",maxx);
}