您的位置:首页 > 产品设计 > UI/UE

HDU3415 - Max Sum of Max-K-sub-sequence - 单调队列

2017-05-07 16:32 423 查看
1.题目描述:


Max Sum of Max-K-sub-sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 8024    Accepted Submission(s): 2929


Problem Description

Given a circle sequence A[1],A[2],A[3]......A
. Circle sequence means the left neighbour of A[1] is A
, and the right neighbour of A
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 

2.题意概述:

要你寻找一个连续子序列,使得他们的和最大,且序列长度不超过K

3.解题思路:

因为序列是环状的,所以可以在序列后面复制前k-1个数字。如果用s[i]来表示复制过后的序列的前i个数的和,那么任意一个子序列[i..j]的和就等于s[j]-s[i-1]。 
对于每一个j,用s[j]减去最小的一个s[i](i>=j-k)就可以得到以j为终点长度不大于k的和最大的序列了。将原问题转化为这样一个问题后,就可以用单调队列解决了。 
单调队列即保持队列中的元素单调递增(或递减)的这样一个队列,可以从两头删除,只能从队尾插入。单调队列的具体作用在于,由于保持队列中的元素满足单调性, 
对于上述问题中的每个j,可以用O(1)的时间找到对应的s[i]。(保持队列中的元素单调递增的话,队首元素便是所要的元素了)。 
维护方法:对于每个j,我们插入s[j-1]的下标,插入时从队尾插入。为了保证队列的单调性,我们从队尾开始删除元素,直到队尾元素对应的值比当前需要插入的s[j-1]小, 
就将当前元素下标插入到队尾。之所以可以将之前的队列尾部元素全部删除,是因为它们已经不可能成为最优的元素了,因为当前要插入的元素位置比它们靠前, 
对应的值比它们小。我们要找的,是满足(i>=j-k)的i中最小的s[i]。在插入元素后,从队首开始,将不符合限制条件(i<j-k)的元素全部删除,此时队列一定不为空。(因为刚刚插入了一个一定符合条件的元素)。 

4.AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#define INF 0x3f3f3f3f
#define maxn 200100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root
4000
].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
int q[maxn], a[maxn], sum[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int t, n, k;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &k);
sum[0] = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
for (int i = n + 1; i <= n * 2; i++)
sum[i] = sum[i - 1] + a[i - n];
int ans = -INF, l = -1, r = -1, head = 0, tail = 0;
q[0] = 0;
for (int i = 1; i <= n + k; i++)
{
while (head <= tail && sum[q[tail]] > sum[i - 1])
tail--;
q[++tail] = i - 1;
while (i - q[head] > k)
head++;
int tmp = sum[i] - sum[q[head]];
if (ans < tmp)
{
ans = tmp;
l = q[head] + 1;
r = i;
}
}
printf("%d %d %d\n", ans, l > n ? l - n : l, r > n ? r - n : r);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm hdu