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. Javascript用数组实现栈和队列

    栈是遵循后进先出(LIFO)规则的一种有序集合,比如桌上的一叠书,我们只能从上面放或取. 队列是遵循先进先出(FIFO)规则的一种有序集合,比如排队,先排到的先离开. 数组也是一种有序的集合,它与上面 ...

  2. H5页面中尝试调起APP

    安卓版本5.0以上 IOS版本10.0以上 采用事件触发的方式处理唤醒APP 市面上常见的功能 这种功能现如今应该非常普遍了,淘宝H5,知乎H5等等... 点击后会调起APP或者打开下载页面或者直接进 ...

  3. 【C++小白成长撸】--N阶幻方(魔阵)矩阵

    解决方法:1.第一个元素放在第一行中间一列 2.下一个元素存放在当前元素的上一行.下一列. 3.如果上一行.下一列已经有内容,则下一个元素的存放位置为当前列的下一行. 在找上一行.下一行或者下一列的时 ...

  4. web程序员标准环境之DreamWeaver【推荐】

    Adobe Dreamweaver,简称"DW",中文名称 "梦想编织者",是美国MACROMEDIA公司开发的集网页制作和管理网站于一身的所见即所得网页编辑器 ...

  5. JAVA基础第七组(5道题)

    31.题目:将一个数组逆序输出.              1.程序分析:用第一个与最后一个交换. package com.niit.homework1; /** * @author: Annie * ...

  6. 201521123019 《Java程序设计》第3周学习总结

    1. 本周学习总结 2. 书面作业 (1)代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...

  7. 201521123047《Java程序设计》第1周学习总结

    1. 本章学习总结 学习到了jdk,jvm,jre之间的关系,下载并安装了jdk,学会设置path变量,初步学会建立简单的java程序,并执行成功.初步学会notepad++,eclipse的操作.学 ...

  8. evak购物车-课程设计(201521123034陈凯欣)

    1.团队课程设计博客链接 https://i.cnblogs.com/EditPosts.aspx?postid=7047127 2.个人负责模块或任务说明 1.Java 编写商品类Goods,商品属 ...

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

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...

  10. linux(CentOS5.8)环境下搭建Radius

    本文记录了freeRadius在CentOS5.8环境下的基本搭建过程,未涉及mysql的加入及配置 freeradius官方地址:http://freeradius.org/ 环境:CentOS5. ...