标签:des c style class blog code
单调队列的应用
#include<map> #include<set> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define inf 0x0f0f0f0f using namespace std; const int maxn=100000+10; int sum[maxn*2],a[maxn]; int main() { int n,k,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&k); sum[0]=0; for (int i=1;i<=n;i++) { scanf("%d",&a[i]); sum[i]=sum[i-1]+a[i]; } for (int i=1;i<=n;i++) sum[n+i]=sum[n+i-1]+a[i]; int start,end,ans=-99999999; deque<int>q; //q.push_back(0); for (int i=1;i<=n+k-1;i++) { while(!q.empty() && sum[i-1]<sum[q.back()]) q.pop_back(); while(!q.empty() && q.front()<i-k) q.pop_front(); q.push_back(i-1); if (sum[i]-sum[q.front()]>ans) { ans=sum[i]-sum[q.front()]; start=q.front()+1; end=i; } } if (end!=n) end%=n; printf("%d %d %d\n",ans,start,end); } return 0; }
作者 chensunrise
hdu 3415 Max Sum of Max-K-sub-sequence,布布扣,bubuko.com
hdu 3415 Max Sum of Max-K-sub-sequence
标签:des c style class blog code
原文地址:http://www.cnblogs.com/chensunrise/p/3766590.html