Max Sum of Max-K-sub-sequence

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

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
 
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[],sum[];
int main()
{
int t,n,k,i,j,ij,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
m=n;
a[]=sum[]=;
for(i=,j=;i<=n;i++,j++)scanf("%d",&a[i]),sum[i]=sum[j]+a[i];
n<<=;
for(ij=;i<=n;i++,j++,ij++)sum[i]=sum[j]+a[ij];
int tail=,top=,maxx=sum[],maxi=,maxj=;
a[tail++]=;
n>>=;
n+=k;
for(i=;i<n;i++)
{
while(tail>top&&i-a[top]>k)top++;
if(maxx<sum[i]-sum[a[top]])
maxx=sum[i]-sum[a[top]],maxi=a[top]+,maxj=i;
while(tail>top&&sum[i]<sum[a[tail-]])tail--;
a[tail++]=i;
}
if(maxj>m)
maxj-=m;
printf("%d %d %d\n",maxx,maxi,maxj);
}
}

Max Sum of Max-K-sub-sequence hdu3415的更多相关文章

  1. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

  2. HDU3415:Max Sum of Max-K-sub-sequence(单调队列)

    Problem Description Given a circle sequence A[1],A[2],A[3]......A[n]. Circle sequence means the left ...

  3. hdu3415 Max Sum of Max-K-sub-sequence

       Max Sum of Max-K-sub-sequence Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64 ...

  4. hdu3415 Max Sum of Max-K-sub-sequence 单调队列

    //hdu3415 Max Sum of Max-K-sub-sequence //单调队列 //首先想到了预处理出前缀和利用s[i] - s[j]表示(j,i]段的和 //之后的问题就转换成了求一个 ...

  5. K - Max Sum Plus Plus

    K - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  6. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  7. Leetcode: Max Sum of Rectangle No Larger Than K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  8. 【leetcode】363. Max Sum of Rectangle No Larger Than K

    题目描述: Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the ma ...

  9. Max Sum of Rectangle No Larger Than K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

随机推荐

  1. Java内存模型_顺序一致性

    数据竞争: 当程序未正确同步时,就会存在数据竞争.java内存模型规范对数据竞争的定义如下: 在一个线程中写一个变量 在另一个线程读同一个变量 而且写和读没有通过同步来排序 如果程序是正确同步的,程序 ...

  2. NSA武器库知识整理

    美国国家安全局(NSA)旗下的"方程式黑客组织"(shadow brokers)使用的部分网络武器被公开,其中包括可以远程攻破全球约70%Windows机器的漏洞利用工具. 其中, ...

  3. 在htnl中,<input tyle = "text">除了text外还有几种种新增的表单元素

    input标签新增属性       <input   list='list_t' type="text" name='user' placeholder='请输入姓名' va ...

  4. JS中 事件冒泡与事件捕获

    [JS中的事件流]  1.事件冒泡:当某DOm元素触发一种事件时,会从当前节点开始,逐级往上触发其祖先节点的同类型事件,直到DOM根节点:   >>>什么情况下会产生事件冒泡 ① D ...

  5. 分而治之(Work Breakdown Structure, WBS)

    不知道大家有没有和我一样的情况,就是想写一篇博客,不知道从何写起,如何组织语言,如何安排这篇博客的要交待的事情的前因后果:如果在写作过程中被打断,又不知道如何重新拾起键盘,从哪里写起."就如 ...

  6. 201521123006 《java程序设计》 第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 class ArrayAlg { public static < ...

  7. 201521123007《Java程序设计》第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 2. 书面作业 1. clone方法 1.1 Object ...

  8. vbs文件共享变量与函数的方法

    参考资料: vbs能否像其他编程语言一样,把写好的代码打包成类库以供调用呢?经过搜索和实验,发现vbs文件之间可以互相调用并共享变量,这样我们就不用再反复地编写轮子了. 以下是一个调用实例: ==== ...

  9. 201521123005《Java程序设计》第十三周学习总结

    1. 本周学习总结 1.网络基本概念 2.网络常用命令 ipconfig 查看网络配置信息,如ip地址 ping 测试网络是否连通 telnet 远程登录 ftp 登陆ftp Fport (xp下)查 ...

  10. 201521123052《Java程序设计》第10周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...