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. JDBCTemplate

    1.Spring提供的一个操作数据库的技术JdbcTemplate,是对Jdbc的封装.语法风格非常接近DBUtils. JdbcTemplate可以直接操作数据库,加快效率,而且学这个JdbcTem ...

  2. 关于request和response的中文乱码问题

    相信大家在开发Web项目中都会遇到中文的请求乱码和响应乱码的情况,现在给大家梳理一下并提供解决方案. 1.为什么会出现乱码: 出现乱码的根本原因是浏览器和服务器的解码方式不一致引起的.所以我们统一编码 ...

  3. LVS-NAT搭建HTTP及HTTPS

    author:JevonWei 版权声明:原创作品 搭建NAT模式的HTTP环境 网络拓扑图如下 网络环境 RS1 192.168.198.138 RS2 192.168.198.120 LVS: D ...

  4. websocket实现简单聊天程序

    程序的流程图: 主要代码: 服务端 app.js 先加载所需要的通信模块: var express = require('express'); var app = express(); var htt ...

  5. 搭建自己的CA服务 - OpenSSL CA 实战

    当前网络安全事故不断,如何提升系统安全性是一个系统上线之前必须考虑的重点DFx特性之一.在提升系统安全性的方法中, 给每个端口(通道)加上SSL协议是最通用和有效的一种. 使用SSL就必须要有证书,在 ...

  6. SNS团队Beta阶段第五次站立会议(2017.5.26)

    1.立会照片 2.每个人的工作 成员 今天已完成的工作 明天计划完成的工作 罗于婕 生词本功能测试,bug修复 发音图标的改进 龚晓婷 辅助完善历史纪录的功能 对于历史记录功能的测试 林仕庄 继续完善 ...

  7. 201521123028 《java程序设计》 第7周学习总结

    1. 本周学习总结 2. 书面作业 Q1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 Ans: ArrayList的contains源代码 contain源代码 ...

  8. 201521123075 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. - 了解了多态就是以单一的接口操作多种类型的对象,但是对多态和继承的关系还是有点混乱. - ...

  9. 201521123071《Java程序设计》第1周学习总结

    1. 本章学习总结 通过本周的学习,对java的一些语法以及java的发展史有了一定的基础认识,也了解了JDK的安装,以及环境变量定义和配置等知识.还有对码云,Markdown等的使用,大大方便了我们 ...

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

    1. 本章学习总结 1.java是一个面向对象的编程语言,相对于c++来说代码较简便又好用.第一次接触java时感觉每句代码比较难写,但学习了一些快捷方法后就很方便了. 2.java运行于JVM,因此 ...