HDU_3415
Because the original sequence is a ring, in order to facilitate the processing, the K-1 elements can be supplemented at the end of the sequence, thus becoming a chain. If I is set to A [I], f [I] is the largest sum of the right boundary over item I, then f [I] = max {A [I]-A [j]} (I-K <= j <I ), so we can use A monotonous queue to maintain the smallest A [j] that we have encountered so far, and I-K <= j <I.
Because the sequence is not empty (or j <I) is required, the insert operation should be done at the end, at the same time, you must insert an element with a label of 0 and a value of 0 into the queue at the beginning.
#include<stdio.h>
#include<string.h>
#define MAXD 200010
#define INF 1000000000
int N, K, f[MAXD], x[MAXD], A[MAXD], q[MAXD], d[MAXD];
void init()
{
int i, j;
scanf("%d%d", &N, &K);
for(i = 1; i <= N; i ++)
scanf("%d", &x[i]);
A[0] = 0;
for(i = 1; i <= N; i ++)
{
A[i] = x[i];
A[i] += A[i - 1];
}
for(i = N + 1; i < N + K; i ++)
{
A[i] = x[i - N];
A[i] += A[i - 1];
}
}
void solve()
{
int i, j, front, rear, a, b, max, sum;
front = rear = 0;
max = - INF;
d[rear] = 0;
q[rear] = 0;
rear ++;
for(i = 1; i < N + K; i ++)
{
while(front < rear && d[front] < i - K)
front ++;
sum = A[i] - A[d[front]];
if(sum > max)
{
max = sum;
a = d[front];
b = i;
}
while(front < rear && q[rear - 1] > A[i])
rear --;
q[rear] = A[i];
d[rear] = i;
rear ++;
}
if(b > N)
b -= N;
printf("%d %d %d\n", max, a + 1, b);
}
int main()
{
int t;
scanf("%d", &t);
while(t --)
{
init();
solve();
}
return 0;
}