因为是circle sequence,可以在序列最后+序列前n项(或前k项);利用前缀和思想,预处理出前i个数的和为sum[i],则i~j的和就为sum[j]-sum[i-1],对于每个j,取最小的sum[i-1],这就转成一道单调队列了,维护k个数的最小值。

----------------------------------------------------------------------------------

#include<cstdio>
#include<deque>
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
const int maxn=100000*2+5;
const int inf=1<<30;
int sum[maxn];
deque<int> q;
deque<int> num;
int main()
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
int kase;
scanf("%d",&kase);
while(kase--) {
sum[0]=0;
int n,k,t;
scanf("%d%d",&n,&k);
Rep(i,1,n) {
scanf("%d",&t);
sum[i]=sum[i-1]+t;
}
Rep(i,1,n) sum[i+n]=sum[n]+sum[i];
while(!q.empty()) { q.pop_back(); num.pop_back(); }
int ans[3]={-inf,0,0};
rep(i,n+n) {
if(i && sum[i]-q.front()>ans[0]) {
ans[0]=sum[i]-q.front();
ans[1]=num.front()+1; ans[2]=i;
}
if(!q.empty()) {
if(num.front()+k<i+1) { q.pop_front(); num.pop_front(); }
while(!q.empty() && q.back()>=sum[i]) { 
   q.pop_back();
num.pop_back(); 
}
}
q.push_back(sum[i]);
num.push_back(i);
}
if(ans[2]>n) ans[2]%=n;
printf("%d %d %d\n",ans[0],ans[1],ans[2]);
}
return 0;
}

----------------------------------------------------------------------------------

Max Sum of Max-K-sub-sequenceTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6213    Accepted Submission(s): 2270

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 

HDOJ 3415 Max Sum of Max-K-sub-sequence(单调队列)的更多相关文章

  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. hdu 3415 单调队列

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

  3. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

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

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

  5. POJ 3415 Max Sum of Max-K-sub-sequence (线段树+dp思想)

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

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

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

  7. hdu 3415 Max Sum of Max-K-sub-sequence 单调队列。

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

  8. [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 ...

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

随机推荐

  1. Unix/Linux环境C编程入门教程(3) Oracle Linux 环境搭建

    Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 2010年9月,Oracle Enterprise Linux发布新版内核--Un ...

  2. 解决android开发webservice的发布与数据库连接的问题

    由于app后续开发的需要,移植了两次webservice和数据库,遇到了不少问题,也花费了很多时间,实践告诉我要学会寻找问题的根源,这样才能在开发中节省时间,尽快解决问题!好,废话不多说,转入正题…… ...

  3. 多个Activity之间的切换与数据交互

    总结 两个activity之间切换我概括的分为两步: 1. 代码实现切换操作.2.配置中声明另外一个acitivity! 1. 代码实现切换操作 显示定义一个intent 对象,Intent 这个类的 ...

  4. 使用my97datepicker控件实现日期范围选择

    注:(2014-12-05内容修改:添加运行效果) 使用my97datepicker 控件,需要对日期的范围进行控制,本人自己写了一个js完成此功能,示例为当前日期到下一周周五之间的日期可选,其他日期 ...

  5. C++ Primer 读书笔记 第2章 变量和基本类型

    C++ Primer 第二章 变量和基本类型 2.1 基本内置类型 C++定义了一组表示整数.浮点数.单个字符和布尔值的算术类型(arithmetic type),此外还定义了Void类型. 算术类型 ...

  6. if---(switch-case)语句初步学习总结

    Daily sentence:  Happiness is about having each tiny wish come true. 幸福就是达成每一个Tiny Wish. Ctrl+E D C# ...

  7. switch case default 的使用

    switch_case从页面输入五个同学的成绩,求出平均成绩,如果大于等于90为优秀,小于90大于等于80为良好,小于80大于等于70为一般,小于70大于等于60为较差,小于60为很差 SWITCH语 ...

  8. JavaScript对css样式表操作

    CSS样式表3种方式: 内嵌:写在html标签中的样式 :如:<p style="width:100px"> 内嵌</p> 内联:写在html 中<h ...

  9. asp.net mvc 对象和集合序列化

    private List<Model.Organization.Organization> GetOrganiztions(){var organizations = new List&l ...

  10. 获取 web容器中的bean

    public class WebContextBeanFinder { public static Object getBean(String beanId) { ServletContext ser ...