Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
98263 | CSYZCaoMY | 早凉与序列4 | C++ | 通过 | 100 | 115 MS | 7488 KB | 2090 | 2023-08-14 13:39:29 |
#include<bits/stdc++.h> using namespace std; long long a[200005],s[200005],t[800005]; inline void pushup(int now){ t[now]=min(t[now<<1],t[now<<1|1]); } void build(int now,int l,int r){ if(l==r){ t[now]=s[l]; return; } int mid=(l+r)>>1; build(now<<1,l,mid); build(now<<1|1,mid+1,r); pushup(now); } void add(int po,long long val,int now,int l,int r){ if(l==r){ t[now]+=val; return; } int mid=(l+r)>>1; if(po<=mid) add(po,val,now<<1,l,mid); else add(po,val,now<<1|1,mid+1,r); pushup(now); } long long query(int l,int r,int now,int nl,int nr){ if(l==nl && r==nr) return t[now]; int mid=(nl+nr)>>1; if(r<=mid) return query(l,r,now<<1,nl,mid); else if(l<=mid) return min(query(l,mid,now<<1,nl,mid),query(mid+1,r,now<<1|1,mid+1,nr)); else return query(l,r,now<<1|1,mid+1,nr); } int main(){ int n; long long x,ans=0,k; scanf("%d%lld%lld",&n,&k,&x); if(x<0) x=-x,k=n-k; for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); s[i]=s[i-1]+a[i]; } build(1,0,n); for(long long r=1;r<=k;r++){ add(r-1,(r-1)*x,1,0,n); long long minn=query(0,r-1,1,0,n); ans=max(ans,s[r]-minn+r*x); } for(long long r=k+1;r<=n;r++){ long long minn; if(k){ add(r-1,(r-1)*x,1,0,n); minn=query(r-k,r-1,1,0,n); ans=max(ans,s[r]-minn+r*x); } add(r-k,-2*r*x,1,0,n); minn=query(0,r-k,1,0,n); ans=max(ans,s[r]-minn-r*x); } printf("%lld",ans); return 0; }/* Samples 1: Input: 4 1 2 2 -1 2 3 Output: 5 Samples 2: Input: 6 2 -8 4 -1 9 -3 7 -8 Output: 44 Samples 3: Input: 3 0 5 3 2 4 Output: 0 Samples 4: Input: 8 4 873724514 -280880159 -989243847 -340549498 -901829651 942399205 -404025657 573646300 -199164666 Output: 4407753238 Hack 1: Input: 8 6 -458428052 -428631146 548611225 -23661324 -995454713 -944507658 975884481 896412548 661325918 Output: 2992050999 */