Max Sum of Max-K-sub-sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5335    Accepted Submission(s): 1939

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
Author
shǎ崽@HDU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3423 3417 3418 3419 3421 
 
 
 #include<iostream>
#include<stdio.h>
#include<cstdlib>
#include<cstring>
#include<cstdlib>
using namespace std; int a[],s[];
int head,tail,len,n,k;
typedef struct
{
int sum;
int s,e;
}Queue;
Queue q[],tom,tmp; void Init()
{
int i;
for(i=;i<=n;i++)
scanf("%d",&a[i]);
len=n+k;
for(i=n+;i<=len;i++)
a[i]=a[i-n];
for(s[]=,i=;i<=len;i++)
s[i]=a[i]+s[i-];
n=n+k;
len=len-k;
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
Init();
head=;tail=;
tom.sum=s[];tom.s=;tom.e=;
q[]=tom;
for(i=;i<=n;i++)
{
tmp.sum=s[i];
tmp.s=;
tmp.e=i;
while( head<=tail && q[tail].sum>tmp.sum ) tail--;
q[++tail]=tmp;
while( head<=tail && q[head].e+k<tmp.e ) head++; if(tmp.sum-q[head].sum>tom.sum && tmp.e!=q[head].e)
{
tom.sum=tmp.sum-q[head].sum;
tom.s=q[head].e+;
tom.e=tmp.e;
}
else if( i<=k && tmp.sum>tom.sum)
{
tom=tmp;
}
}
printf("%d",tom.sum);
if( tom.s>len ) tom.s-=len;
if( tom.e>len ) tom.e-=len;
printf(" %d %d\n",tom.s,tom.e);
}
return ;
}

最新文章

  1. 用sass写栅格系统
  2. HTTP/1.1 中 If-Modified-Since 和 If-Unmodified-Since 区别简记
  3. ie8中支持 password 的 placeholder
  4. JVM 关闭前执行命令的钩子
  5. CAF(C++ actor framework)(序列化之复杂类,分析 还有自己不懂的细思恐极函数实现)(三)
  6. Android常用错误解决汇总
  7. 关于 Swift
  8. JavaScript检测提交表单text合法
  9. 第九周博客作业&lt;西北师范大学|李晓婷&gt;
  10. 动态更新Icon
  11. poj 2240 Arbitrage(最短路问题)
  12. Linux下使用NTFS格式移动硬盘
  13. 一道非常易错的js面试题
  14. OrmLite动态创建表,一个实体类创建多张表的的偏招
  15. [z] How can we render CSS3 in a WebBrowser Control ?
  16. NDK编程jni学习入门,声明native方法,使其作为java与c的交互接口
  17. Metro应用Json数据处理
  18. (转)用python实现抓取网页、模拟登陆
  19. 深度增强学习--Deep Q Network
  20. PAT 1072. 开学寄语(20) JAVA

热门文章

  1. tushare模块的应用
  2. [Objective-C语言教程]日志处理(21)
  3. Unicode字符串索引
  4. Es6 类class的关键 super、static、constructor、new.target
  5. Linux修改profile文件改错了,恢复的方法
  6. mysql 与sqlser group by
  7. 原 form 表单中 disabled 属性的元素不参与表单提交
  8. TX2 i2c-tools使用
  9. Sequential Minimal Optimization(SMO,序列最小优化算法)初探
  10. testng XMl 参数化