HDOJ 3415 Max Sum of Max-K-sub-sequence(单调队列)

因为是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

HDOJ Monthly Contest – 2010.06.05

Recommend

lcy   |   We have carefully selected several similar problems for you:  3423 3417 3418 3419 3421 

时间: 2024-12-13 18:55:44

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

hdu3415——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 (Java/Others) Total Submission(s): 6130    Accepted Submission(s): 2234 Problem Description Given a circle sequence A[1],A[2],A[3]......A[n]. Circle s

HDU 1003 Max Sum(dp,最大连续子序列和)

Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input

HDU 1003.Max Sum【最大连续子序列和】【8月14】

Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input

POJ 1003 Max Sum

Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input

hdu 3415 单调队列

Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5690    Accepted Submission(s): 2059 Problem Description Given a circle sequence A[1],A[2],A[3]......A[n]. Circle s

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

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3415 题意:给出一个数环,要找出其中9长度小于等于K的和最大的子段. 思路:不能采用最暴力的枚举,题目的数据量是10^5,O(N^2)的枚举回去超时,本题采用的很巧妙的DP做法,是用单调队列优化的DP. 运用的是STL的deque,从i:1~a找到以其中以i为尾的符合条件的子段,并将i本身放入双向队列,所有i从队列后放入,保证了队列的单调性. 代码: #include <iostream> #incl

HDU 3415 Max Sum of Max-K-sub-sequence 单调队列题解

本题又是一题单调队列题解. 技巧就是需要计算好前n项和Sn = a1 + a2 + ... an 这样方便处理. 记录一条单调队列,其意义是: q(head), q(head+1), ...q(tail) 其中头q(head)代表当前最佳解的起点 这样我们只需要在求某点为结尾的S[i] - S[q(head)就得到当前最佳值. 了解了单调数列,知道其中的记录意义,那么这道题就没有难度了.我也是了解这些信息之后就自己敲出代码的. 不过有些细节没写好也让我WA了几次. 最近少刷水题,而一直都是每天一

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

题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的和为sum[i]. 然后问题变成了求一个max{sum[i]-sum[j]}(i-k<j<i) 意思就是对于每一个sum[i],我们只需要找一个满足条件的最小的sum[j],然后我们就可以用一个单调队列来维护. 1 #include<bits/stdc++.h> 2 #define F

HDU 3415 Max Sum of Max-K-sub-sequence

转载请注明出处:http://blog.csdn.net/u012860063 Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5791    Accepted Submission(s): 2083 Problem Description Given a circle seq