您的位置:首页 > 产品设计 > UI/UE

HDU 3415——Max Sum of Max-K-sub-sequence

2012-06-15 17:18 267 查看
[align=left]Problem Description[/align]
Given a circle sequence A[1],A[2],A[3]......A
. Circle sequence means the left neighbour of A[1] is A
, and the right neighbour of A
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.

[align=left]Input[/align]
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).

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

7 1 3
7 1 3
7 6 2
-1 1 1


[align=left]Author[/align]
shǎ崽@HDU

[align=left]Source[/align]
HDOJ Monthly Contest – 2010.06.05

[align=left]Recommend[/align]
lcy

【算法分析】

和 1003 Max Sum 差不多,不同之处有两点;

1、数组是循环的

2、限制了最长的连续字段的长度为 K

还是最原始的想法:找最大的 s[i] - s[j] 只不过 i - K <= j < i 了

那么还是可以用单调队列优化,为什么?

区间:[i - K , i] 是单调的

接着的东西就跟 1003 差不多了

PS:由于数组定义小了,硬是WA了将近一个小时!以后一定要注意数据的大小!!!

下面的代码仅供参考:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x7fffffff
using namespace std;
int main()
{
int T,N,K,st,en,Max;
int a[200005],q[200005],head,tail;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&K);
a[0]=0;
for(int i=1;i<=N;i++)
{
scanf("%d",&a[i]);
a[i]+=a[i-1];
}
for(int i=1;i<=K;i++)
a[N+i]=a
+a[i];
head=tail=0;
q[++tail]=0;
Max=-INF;
for(int i=1;i<=N+K;i++)
{
while(head<tail && q[head+1]<i-K) head++;
if(a[i]-a[q[head+1]]>Max)
{
Max=a[i]-a[q[head+1]];
st=q[head+1]+1;
en=i;
}
while(head<tail && a[q[tail]]>a[i]) tail--;
q[++tail]=i;

}
st=(st-1)%N+1;
en=(en-1)%N+1;
printf("%d %d %d\n",Max,st,en);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: