• 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): 7034    Accepted Submission(s): 2589


    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
     
    Author
    shǎ崽@HDU
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  3423 3417 3418 3419 3421 
     
    题解:让找距离小于等于k的连续一段区间的值最大,数据是环状的;
    要用单调队列;本来想着贪心,果断wa,单调队列注意,找到前缀和,现在只需要根据r找l,l在单调队列里找,单调队列要保证从小到大。这样就保证了队头是最小值,注意单调队列放的是i(初态),所以要放i - 1;这点错了好久。当前的位置是终态,也就是i
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int MAXN = 1000100;
    int num[MAXN << 1];
    int Q[MAXN << 1];
    int sum[MAXN << 1];
    int main(){
        int T, n, k;
        scanf("%d", &T);
        while(T--){
            scanf("%d%d", &n, &k);
            int head = 0, tail = -1;
            sum[0] = 0;
            for(int i = 1; i <= n; i++){
                scanf("%d", num + i);
                sum[i] = sum[i - 1] + num[i];
            }
            for(int i = n + 1; i <= n + k; i++){
                num[i] = num[i - n];
                sum[i] = sum [i - 1] + num[i];
            }
            int ans = -0x3f3f3f3f, L = 0, R = 0;
            for(int i = 1; i < n + k; i++){
                while(head <= tail && sum[i - 1] < sum[Q[tail]])
                    tail--;
                while(head <= tail && i - Q[head]> k)head++;
                Q[++tail] = i - 1;
                if(sum[i] - sum[Q[head]] > ans){
                    ans = sum[i] - sum[Q[head]];
                    L = Q[head] + 1;
                    R = i;
                }
            }
            printf("%d %d %d
    ", ans, L>n?L-n:L, R>n?R-n:R);
        }
        return 0;
    }

    贪心wa;由于规定了最大长度,这样贪心就不行了;

    wa代码 :

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int MAXN = 1000010;
    int num[MAXN];
    int main(){
        int T;
        int n, k;
        scanf("%d", &T);
        while(T--){
            scanf("%d%d", &n, &k);
            for(int i =0 ; i < n; i++){
                scanf("%d", num + i);
            }
            for(int i = n; i < 2 * n; i++)
                num[i] = num[i - n];
                int l = 0, r = 0, ans = -0x3f3f3f3f, cur = 0, L = 0, R = 0, cnt = 0;
            for(int i = 0; i < 2 * n; i++){
                cur += num[i];
                cnt++;
                if(cur <= num[i] || cnt > k){
                    l = i;
                    cur = num[i];
                    cnt = 1;
                }
    
                r = i;
                if(cur > ans){
                    L = l;
                    R = r;
                    ans = cur;
                }
            }
            printf("%d %d %d
    ", ans, L + 1 > n ? L + 1 - n: L + 1, R + 1 > n ? R + 1 - n: R + 1);
        }
        return 0;
    }
  • 相关阅读:
    百度之星初赛 A
    百度之星 初赛 BC
    2016 百度之星资格赛
    codeforces 749
    codeforces 785
    HDU 4617
    网络流 poj 2195
    网络流 poj 3436 poj 3281
    codeforces 780 C
    idea激活
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5524498.html
Copyright © 2020-2023  润新知