题目描述:
给出一个长度为n(1<=n<=100000)的整数序列,选择长度不超过k(1<=k<=n)的段,使得总和最大。
分析:
单调队列啦
代码:
const
maxn=200000;
var
data,num,a,f:array [0..maxn] of int64;
ans:int64;
n,m,head,tail,i,j:longint;
begin
readln(n,m);
for i:=1 to n do
read(a[i]);
num[1]:=a[1];
for i:=2 to n do
num[i]:=num[i-1]+a[i];
head:=1;tail:=1;data[head]:=0;
for i:=1 to n do
begin
while (data[head]<i-m) and (tail>=head) do
inc(head);
while (num[data[tail]]>num[i]) and (tail>=head) do
dec(tail);
inc(tail);
data[tail]:=i;
f[i]:=num[i]-num[data[head]];
end;
for i:=1 to n do
if f[i]>ans then
ans:=f[i];
write(ans);
end.