HDU_3415_单调队列
http://acm.hdu.edu.cn/showproblem.php?pid=3415
初探单调队列,需要注意的是每次i维护的是i-1。
#include<iostream> #include<cstring> #include<cstdio> #include<deque> #define INF 0x3f3f3f3f using namespace std; int a[200005],sum[200005]; deque<int> q; int main() { int T,n,k; scanf("%d",&T); while(T--) { q.clear(); sum[0] = 0; scanf("%d%d",&n,&k); 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[i+n] = sum[i+n-1]+a[i]; int ans = -INF,left,right; int endd = 2*n; for(int i = 1;i < endd;i++) { while(!q.empty() && sum[i-1] < sum[q.back()]) q.pop_back(); if(!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()]; left = q.front()+1; right = i; } } if(right > n) right -= n; printf("%d %d %d\n",ans,left,right); } return 0; }