Max Sum of Max-K-sub-sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4729 Accepted Submission(s): 1723
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
Author
shǎ崽@HDU
Source
HDOJ Monthly Contest – 2010.06.05
Recommend
lcy
单调队列优化(裸的)
做完这题发觉自己完全不会单调队列
首先,sum[i]=min(s[i]-s[j]) (i-k<j<i)
所以要求出一段的最小值,k向左移。
这样就是裸的单调队列
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<cctype>
#include<cassert>
#include<climits>
#include<queue>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define RepD(i,n) for(int i=n;i>=0;i--)
#define MEM(a) memset(a,0,sizeof(a))
#define MEMI(a) memset(a,127,sizeof(a))
#define MEMi(a) memset(a,128,sizeof(a))
#define INF (2139062143)
#define F (1000000009)
#define MAXT (100+10)
#define MAXN (200000+10)
#define MAXAi (1000)
typedef long long ll;
struct node
{
int i,v;
node(){}
node(int _i,int _v):i(_i),v(_v){}
node(int _v):v(_v){}
friend bool operator<(node a,node b){return a.v>b.v;}
// friend bool operator>(node a,node b){return a.v>b.v;}
};
struct mo_q
{
int i,j;
priority_queue <node> q;
mo_q(){i=-1;j=0;}
void ins(int v)
{
while (!q.empty()&&q.top().v>v) q.pop();
q.push(node(++i,v));
}
void pop(){j++;}
int qur()
{
while (!q.empty()&&q.top().i<j) q.pop();
return q.top().i;
}
void clear(){i=-1,j=0;while (!q.empty()) q.pop();}
}q;
int T,n,k,a[MAXN],s[MAXN]={0};
int main()
{
// freopen("hdu3415.in","r",stdin);
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&k);
For(i,n) {scanf("%d",&a[i]);s[i]=s[i-1]+a[i];}
For(i,n) {a[n+i]=a[i];s[n+i]=s[n+i-1]+a[n+i];}
Rep(i,k) q.ins(s[i]);
int ansl=1,ansr=1,ans=s[1];
Fork(i,k,2*n)
{
int j=q.qur();
int nowl=j+1>n?j+1-n:j+1,nowr=i>n?i-n:i,p=s[i]-s[j];
if (p>ans) ansl=nowl,ansr=nowr,ans=p;
else if (p==ans&&nowl<ansl) ansl=nowl,ansr=nowr;
else if (p==ans&&nowl==ansl&&nowr<ansr) ansl=nowl,ansr=nowr;
q.ins(s[i]);
q.pop();
}
cout<<ans<<' '<<ansl<<' '<<ansr<<endl;
q.clear();
}
// while (1);
return 0;
}