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 ;
}

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. Java概念辨析:equals和== equals和hashCode

    1. equals和== ======================================================================================= ...

  2. robot framework学习笔记之十一--第三方库requests详解

    一.安装 Requests 通过pip安装 pip install requests 或者,下载代码后安装: $ git clone git://github.com/kennethreitz/req ...

  3. PHP全局变量与SESSION 漏洞(global 与 session)

    先看这一段简单的代码 <?php session_start();$_SESSION[‘isadmin’]=’yes’;$isadmin=’no’;echo $_SESSION[‘isadmin ...

  4. java使用Redis7--分布式存储并实现sentinel主从自动切换

    前面实现了分布式存储,也实现了sentinel单点故障时主从自动切换,现在还需要一种机制,实现分布式存储下,单点故障时的主从自动切换. Server配置 # cd /usr/redis/src/tes ...

  5. maven web不能创建src/main/java等文件等问题

    我们在创建maven web项目的时候,默认只有src/main/resources这个source folder,我们按照maven结构添加src/main/java和src/test/java等s ...

  6. C#-WebForm-css box-shadow 给边框添加阴影效果

    box-shadow介绍 css3可以使用 box-shadow 属性轻松地为元素添加阴影效果,box-shadow可以设定多组效果,每组参数值以逗号分隔. 语法: box-shadow:x-shad ...

  7. 配置bootstrap环境

    bootstrap是一个优雅,灵活,可扩展的前端工具集,可搭建WEB页面的HTML,CSS,JavaScript工具集,最重要的是它的栅格系统. 这里不做更多的详细介绍具体可参照官方网站:http:/ ...

  8. (转)【干货】MySQL 5.7 多实例(多进程)配置教程

    原文:https://blog.csdn.net/zougen/article/details/79567744 https://klionsec.github.io/2017/09/20/mysql ...

  9. Spring Boot的filter简单使用

    过滤器(Filter)的注册方法和 Servlet 一样,有两种方式:代码注册或者注解注册 1.代码注册方式 通过代码方式注入过滤器 @Bean     public FilterRegistrati ...

  10. 安装TD出现Unknown user name or bad password问题

    在Server 2003 sp2上安装TD8.0 出现Unknown user name or bad password,是因为2003启用了DEP保护.  关闭系统的DEP保护就可以了.  方法如下 ...