标签:string while return 答案 for str algo AC 子序列和
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=1000000; int q[maxn],a[maxn]; int main(){ int n; int m; scanf("%d%d",&n,&m); for (int i=1;i<=n;i++){ scanf("%d",&a[i]); } for (int i=1;i<=n;i++){ a[i]+=a[i-1]; } int l,r; l=r=1; q[l]=0; int ans=-(0x3f3f3f3f); for (int i=1;i<=n;i++){ while(l<=r&&q[l]<i-m) l++;//保证答案的合法性 ans=max(ans,a[i]-a[q[l]]);//因为保证了队列的单调性,所以队头元素为当前最优 while(l<=r&&a[q[r]]>=a[i]) r--;//保证队列的单调性,即保证队头答案最优 q[++r]=i; } printf("%d\n",ans); return 0; }
标签:string while return 答案 for str algo AC 子序列和
原文地址:https://www.cnblogs.com/lmjer/p/8975694.html