因为是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. 截取字符串一之substring

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

  2. 使用PHP实现请求响应和MySql访问

    在iOS开发当中经常需要使用来自后台的数据,所以使用一种很简便的写后台的方法. 首先,安装XAMPP,这是一个集成好的阿帕奇+MySQL环境,点击按钮即可开启服务,不需要进行任何环境配置. 然后,开启 ...

  3. VisualStudio中的代码段

    VS很强大,在这里就不过多说了,在平时码代码时应用代码段会提高我们的编写速度. 举个例子: 比如输入Console.WriteLine (); 传统方法就是一个字母一个字母的输入进去. 如果大家掌握了 ...

  4. JAVA里的String、Timestamp、Date相互转换

    Timestamp转化为String: SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义 ...

  5. shell命令基础

    1.修改密码 使用 passwd 命令修改密码. 该命令如果在 root 用户下执行,则修改的是 root 用户的密码. 2.获取帮助 使用 ls --help 命令获取帮助. [zhanghuiju ...

  6. C++中operator关键字(重载操作符)

    operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名. 这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算 ...

  7. Git基本应用

    1.创建SSH Key $ cd ~/.ssh $ ssh-keygen -t rsa -C "your_email@example.com" 拷贝id_rsa.pub文件到Set ...

  8. WPF使用Log4net.dll库的demo(转载加个人观点)

    原文地址:http://blog.csdn.net/linraise/article/details/50547149 配置文件解析地址:http://blog.csdn.net/pfe_nova/a ...

  9. mysql之数据库特性认识

    最近面试经常被面试官问道关于数据库方面的知识,于是总结一下面试官问的题以及自己对数据库的认识 1.之前百度面试官问了我一个特别基础的sql问题:如何清除表的所有记录,以前在学校做项目开发的时候有使用过 ...

  10. github版本库使用详细图文教程(命令行及图形界面版)

    投稿:mdxy-dxy 字体:[增加 减小] 类型:转载 时间:2015-08-06我要评论 今天我们就来学习github的使用,我们将用它来管理我们的代码,你会发现它的好处的,当然是要在本系列教程全 ...