HDU 3415——Max Sum of Max-K-sub-sequence
来源:互联网 发布:姓氏图片制作软件 编辑:程序博客网 时间:2024/05/17 05:56
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.
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).
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
46 36 -1 2 -6 5 -56 46 -1 2 -6 5 -56 3-1 2 -6 5 -5 66 6-1 -1 -1 -1 -1 -1
Sample Output
7 1 37 1 37 6 2-1 1 1
Author
shǎ崽@HDU
Source
HDOJ Monthly Contest – 2010.06.05
Recommend
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 0x7fffffffusing 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[N]+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;}
- hdu 3415 Max Sum of Max-K-sub-sequence
- hdu 3415 Max Sum of Max-K-sub-sequence
- hdu 3415 Max Sum of Max-K-sub-sequence题解
- hdu 3415 Max Sum of Max-K-sub-sequence
- hdu 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- hdu 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU - 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU 3415——Max Sum of Max-K-sub-sequence
- 单调队列——HDU 3415 Max Sum of Max-K-sub-sequence
- HDU 3415——Max Sum of Max-K-sub-sequence
- hdu3415——Max Sum of Max-K-sub-sequence
- ORA-14450 attempt to access a transactional temp table already in use
- JSP 转换 mht java 代码
- 正则表达式---小节(二)
- java.lang.NoSuchFieldError: deferredExpression异常
- 写脚本不熟练的程序员不是好程序员----小话python(1)
- HDU 3415——Max Sum of Max-K-sub-sequence
- 判断对象是否为空(数据、集合等对象)
- android面试基础总结
- asp.net 中的守护线程.定时器
- 需求
- 一条语句体现增删改的触发器使用过程
- httpclient必须的jar
- XStream 基本用法
- binary,varbinary,image的区别