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 
 
 
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cstdlib>
  6. using namespace std;
  7.  
  8. int a[],s[];
  9. int head,tail,len,n,k;
  10. typedef struct
  11. {
  12. int sum;
  13. int s,e;
  14. }Queue;
  15. Queue q[],tom,tmp;
  16.  
  17. void Init()
  18. {
  19. int i;
  20. for(i=;i<=n;i++)
  21. scanf("%d",&a[i]);
  22. len=n+k;
  23. for(i=n+;i<=len;i++)
  24. a[i]=a[i-n];
  25. for(s[]=,i=;i<=len;i++)
  26. s[i]=a[i]+s[i-];
  27. n=n+k;
  28. len=len-k;
  29. }
  30. int main()
  31. {
  32. int T,i;
  33. scanf("%d",&T);
  34. while(T--)
  35. {
  36. scanf("%d%d",&n,&k);
  37. Init();
  38. head=;tail=;
  39. tom.sum=s[];tom.s=;tom.e=;
  40. q[]=tom;
  41. for(i=;i<=n;i++)
  42. {
  43. tmp.sum=s[i];
  44. tmp.s=;
  45. tmp.e=i;
  46. while( head<=tail && q[tail].sum>tmp.sum ) tail--;
  47. q[++tail]=tmp;
  48. while( head<=tail && q[head].e+k<tmp.e ) head++;
  49.  
  50. if(tmp.sum-q[head].sum>tom.sum && tmp.e!=q[head].e)
  51. {
  52. tom.sum=tmp.sum-q[head].sum;
  53. tom.s=q[head].e+;
  54. tom.e=tmp.e;
  55. }
  56. else if( i<=k && tmp.sum>tom.sum)
  57. {
  58. tom=tmp;
  59. }
  60. }
  61. printf("%d",tom.sum);
  62. if( tom.s>len ) tom.s-=len;
  63. if( tom.e>len ) tom.e-=len;
  64. printf(" %d %d\n",tom.s,tom.e);
  65. }
  66. return ;
  67. }

hdu 3415 Max Sum of Max-K-sub-sequence 单调队列。的更多相关文章

  1. HDU 4123 Bob's Race:树的直径 + 单调队列 + st表

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 题意: 给你一棵树,n个节点,每条边有长度. 然后有m个询问,每个询问给定一个q值. 设dis[ ...

  2. HDU 4193 Non-negative Partial Sums(想法题,单调队列)

    HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...

  3. HDU 5945 / BestCoder Round #89 1002 Fxx and game 单调队列优化DP

    Fxx and game 问题描述   青年理论计算机科学家Fxx给的学生设计了一款数字游戏. 一开始你将会得到一个数\:XX,每次游戏将给定两个参数\:k,tk,t, 任意时刻你可以对你的数执行下面 ...

  4. HDU 3410 &amp;&amp; POJ 3776 Passing the Message 单调队列

    题意: 给定n长的数组(下标从1-n)(n个人的身高,身高各不同样 问:对于第i个人,他能看到的左边最矮的人下标.(假设这个最矮的人被挡住了,则这个值为0) 还有右边最高的人下标,同理若被挡住了则这个 ...

  5. hdu 1003 Max Sum (DP)

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

  6. HDU 1081:To The Max

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  7. hdu 3415 单调队列

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. HDU 3530 单调队列

    题目大意:给你n个数, 让你问你最长的满足要求的区间有多长,区间要求:MAX - MIN >= m && MAX - MIN <= k 思路:单调队列维护递增和递减,在加入 ...

  9. HDU 3415 Max Sum of Max-K-sub-sequence 最长K子段和

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=3415 意甲冠军:环.要找出当中9长度小于等于K的和最大的子段. 思路:不能採用最暴力的枚举.题目的数据量是 ...

  10. hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)

    题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...

随机推荐

  1. bzoj 2244 [SDOI2011]拦截导弹(dp+CDQ+树状数组)

    传送门 题解 看了半天完全没发现这东西和CDQ有什么关系…… 先把原序列翻转,求起来方便 然后把每一个位置表示成$(a,b,c)$其中$a$表示位置,$b$表示高度,$c$表示速度,求有多少个位置$a ...

  2. 使用textarea标签代替input标签可以实现输入框的大小调节,自动换行,滚动条显示

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Python FLask Web 学习笔记:jinjia2的使用方法1

    # coding:utf-8 from jinja2 import Template x = """ <p>大爷的孙子</p> <ul> ...

  4. 理解 atime,ctime,mtime (下)

    话不多说,开始下篇. # 前言 通过 "理解 atime,ctime,mtime (上)" 我们已经知道了atime 是文件访问时间:ctime是文件权限改变时间:mtime是文件 ...

  5. Spring 并发事务的探究

    前言 在目前的软件架构中,不仅存在单独的数据库操作(一条SQL以内,还存在逻辑性的一组操作.而互联网软件系统最少不了的就是对共享资源的操作.比如热闹的集市,抢购的人群对同见商品的抢购由一位售货员来处理 ...

  6. FFmpeg工具使用总结

    . 一. FFmpeg是什么? 简单说,FFmpeg就是一个很好的,免费的,开源的视频转换工具.详细说,FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依 ...

  7. 39.oracle高级篇

    标题说是高级篇,其实也就是相对于基础篇来说的,也不是很深奥,自己平时工作中也都会用到,这里回忆的并不是特别冷门的知识,不要掉以轻心,以为“高级”就觉得工作中不会用到了. 一.select into 和 ...

  8. C#-WebForm-GridView表格展示数据

    GrideView 控件,功能是将数据库的数据用表格的形式展示在页面上 一.<源>代码中放入 GridView 控件 打开<设计>界面 二.绑定数据源 (一)创建 LinQ 类 ...

  9. Objective-C语法之类和对象

    https://blog.csdn.net/totogo2010/article/details/7708731 Objective-C语法之类和对象 2012年07月02日 17:19:42 知行合 ...

  10. Ubuntu18.04配制阿里巴巴的源

    配制阿里巴巴的源步骤 使用阿里巴巴的开源镜像:https://opsx.alibaba.com/mirror 然后选择ubuntu的帮助选项,复制ubuntu18.04镜像源 设置root账户密码: ...