HDU 3415 Max Sum of Max-K-sub-sequence
来源:互联网 发布:淘宝开店手机端 编辑:程序博客网 时间:2024/06/05 14:48
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
很无语,就是不知道怎么错了,wr了几遍,决定以后还是用STL库吧,数组老是不知道哪出个错误,如果用库函数的话,注意要用deque,因为头也得去掉
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<math.h>using namespace std;#define ll long longconst int INF=0x7fffffff;const int N=100000+2;int v[N],q[2*N],sum[2*N],n,k;int main(){ //若表示区间[a,b],用sum[b]-sum[a-1] int t; scanf("%d",&t); while (t--) { int i; memset(q,0,sizeof(0)); sum[0]=0; scanf("%d%d",&n,&k); for (i=1;i<=n;i++) { scanf("%d",&v[i]); sum[i]=sum[i-1]+v[i]; } for (i=n+1;i<n+k;i++) sum[i]=sum[i-1]+v[i-n]; int s=0,e=0,ans=-INF; int l=0,r=-1; for (i=1;i<n+k;i++) { while (l<=r&&sum[i-1]<sum[q[r]]) r--; q[++r]=i-1; //注意这里哈,就是这wr了几遍,敲习惯了,直接按着模板上了 while (l<=r&&q[l]<i-k) l++; if (sum[i]-sum[q[l]]>ans) { ans=sum[i]-sum[q[l]]; s=q[l]+1; //注意这里 e=i; } } if (e>n) e-=n; printf("%d %d %d\n",ans,s,e); } return 0;}
0 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 Max Sum of Max-K-sub-sequence(单调队列)
- Max Sum of Max-K-sub-sequence HDU
- Max Sum of Max-K-sub-sequence
- Max Sum of Max-K-sub-sequence
- 校招——面试(Android岗)总结
- expect的简单使用,linux交互性shell
- KMP算法实现原理
- Name That Number(模拟)(USACO)
- (HDU1242)Rescue
- HDU 3415 Max Sum of Max-K-sub-sequence
- 刘某某研究生之路
- Genymotion终于复活了
- 51nod oj 1191 消灭兔子 【vector 二分+贪心】
- Spark 中的二次排序Java实现
- 建模
- 第3周项目2-建设“顺序表”算法库
- (HDU1010)Tempter of the Bone
- AsyncTask简单使用(下载图片)