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): 6844    Accepted Submission(s): 2518

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
 题解:
让找环形序列长度小于等于k的最长字串合;
用到了单调队列:
思路:由于是连续的,我们可以对于每一个尾部,找最优的头;那么这个头需要用单调队列来维护;当然这个头要想最优满足两个条件:
1:长度小于等于k;
2:这个头要足够小;
足够小,所以用单调队列;每次插入的值比队列里面的值都大,比当前插入值大的全部不要;

/********/

单调队列即保持队列中的元素单调递增(或递减)的这样一个队列,可以从两头删除,只能从队尾插入。单调队列的具体作用在于,由于保持队列中的元素满足单调性,对于上述问题中的每个j,可以用O(1)的时间找到对应的s[i]。(保持队列中的元素单调增的话,队首元素便是所要的元素了)。

维护方法:对于每个j,我们插入s[j-1](为什么不是s[j]? 队列里面维护的是区间开始的下标,j是区间结束的下标),插入时从队尾插入。为了保证队列的单调性,我们从队尾开始删除元素,直到队尾元素比当前需要插入的元素优(本题中是值比待插入元素小,位置比待插入元素靠前,不过后面这一个条件可以不考虑),就将当前元素插入到队尾。之所以可以将之前的队列尾部元素全部删除,是因为它们已经不可能成为最优的元素了,因为当前要插入的元素位置比它们靠前,值比它们小。我们要找的,是满足(i>=j-k+1)的i中最小的s[i],位置越大越可能成为后面的j的最优s[i]。

在插入元素后,从队首开始,将不符合限制条件(i>=j-k+1)的元素全部删除,此时队列一定不为空。(因为刚刚插入了一个一定符合条件的元素)

ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x) scanf("%lf",&x)
#define P_ printf(" ")
typedef long long LL;
const int MAXN=200010;
int m[MAXN],a[MAXN];
int main(){
    int T,N,K;
    SI(T);
    while(T--){
        SI(N);SI(K);
        int MOD=N;
        for(int i=1;i<=N;i++)SI(a[i]),m[i]=m[i-1]+a[i];
        for(int i=N+1;i<=2*N-1;i++)m[i]=m[i-1]+a[i-N];
        int ans=-INF,l,r;
        N=N+K-1;
        deque<int>q;
        q.clear();
        for(int i=1;i<=N;i++){
            while(!q.empty()&&m[i-1]<m[q.back()])q.pop_back();
            /*为了保证队列的单调性,我们从队尾开始删除元素,
            直到队尾元素比当前需要插入的元素优(本题中是值比待插入元素小,
            位置比待插入元素靠前,不过后面这一个条件可以不考虑),
            就将当前元素插入到队尾。之所以可以将之前的队列尾部元素全
            部删除,是因为它们已经不可能成为最优的元素了,因为当前要插
            入的元素位置比它们靠前,值比它们小。*/
            while(!q.empty()&&q.front()+K<i)q.pop_front();
            q.push_back(i-1);
            if(m[i]-m[q.front()]>ans){
                ans=m[i]-m[q.front()];
                l=q.front()+1;
                r=i;
            }
        }
        printf("%d %d %d
",ans,l%MOD?l%MOD:MOD,r%MOD?r%MOD:MOD);
    }
    return 0;
}
暴力超时;
超时代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x) scanf("%lf",&x)
#define P_ printf(" ")
typedef long long LL;
const int MAXN=100010;
int m[MAXN];
int main(){
    int T,N,K;
    SI(T);
    while(T--){
        SI(N);SI(K);
        for(int i=1;i<=N;i++)SI(m[i]);
        int cur=0,ans=-INF,l,r;
        for(int i=1;i+K-1<=N;i++){
            cur=0;
            for(int j=0;j<K;j++){
                cur+=m[i+j];
                if(cur>ans){
                    ans=cur;
                    l=i;r=i+j;
                }
            }
        }
        printf("%d %d %d
",ans,l,r);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/handsomecui/p/5205202.html