hdoj 3415 Max Sum of Max-K-sub-sequence 【单调队列】

题目链接:hdoj 3415 Max Sum of Max-K-sub-sequence

Max Sum of Max-K-sub-sequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6975 Accepted Submission(s): 2565

Problem Description
Given a circle sequence A[1],A[2],A[3]……A[n]. Circle sequence means the left neighbour of A[1] is A[n] , and the right neighbour of A[n] is A[1].
Now your job is to calculate the max sum of a Max-K-sub-sequence. Max-K-sub-sequence means a continuous non-empty sub-sequence which length not exceed K.

Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases.
Then T lines follow, each line starts with two integers N , K(1<=N<=100000 , 1<=K<=N), then N integers followed(all the integers are between -1000 and 1000).

Output
For each test case, you should output a line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the minimum start position, if still more than one , output the minimum length of them.

Sample Input
4
6 3
6 -1 2 -6 5 -5
6 4
6 -1 2 -6 5 -5
6 3
-1 2 -6 5 -5 6
6 6
-1 -1 -1 -1 -1 -1

Sample Output
7 1 3
7 1 3
7 6 2
-1 1 1

题意:在环上有n个元素,让你找到最大的连续子序列(长度<=k)。输出起点和终点。

思路:就是维护升序的前缀和,不过需要从最前面去掉元素,用个队列维护就可以了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 +10;
const int INF = 0x3f3f3f3f;
int a[MAXN], Queue[MAXN];
int main()
{
    int t; scanf("%d", &t);
    while(t--) {
        int n, k; scanf("%d%d", &n, &k);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        for(int i = n+1; i <= 2*n; i++) {
            a[i] = a[i-n];
        }
        a[0] = 0;
        for(int i = 1; i <= 2*n; i++) {
            a[i] += a[i-1];
        }
        int head = 0, tail = 0;
        int ans = -10000, len = 2*n+10, s, t; Queue[tail++] = 0;
        for(int i = 1; i <= 2*n; i++) {
            while(tail > head && a[Queue[tail-1]] >= a[i]) {
                tail--;
            }
            Queue[tail++] = i;
            if(i == Queue[head]) {
                if(ans < a[i] - a[Queue[head]-1]) {
                    ans = a[i] - a[Queue[head]-1];
                    s = i; t = i; len = t - s + 1;
                }
                continue;
            }
            while(tail > head && i - Queue[head] > k) {
                head++;
            }
            if(ans < a[i] - a[Queue[head]] || (ans == a[i] - a[Queue[head]] && len > i - Queue[head])) {
                ans = a[i] - a[Queue[head]];
                s = Queue[head] + 1; t = i;
                len = t - s + 1;
            }
        }
        if(s > n) s -= n; if(t > n) t -= n;
        printf("%d %d %d\n", ans, s, t);
    }
    return 0;
}

你可能感兴趣的:(hdoj 3415 Max Sum of Max-K-sub-sequence 【单调队列】)