码迷,mamicode.com
首页 > 其他好文 > 详细

hdu3415 单调队列

时间:2016-07-11 22:35:14      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

Max Sum of Max-K-sub-sequence
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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
 
 
题意:
一个环,求长度小于等于k的连续子串值最大。
思路:
对于环,一般的处理就是在后面重复添加一段,求前缀和。然后维护一个递增的队列,如果当前的值比队尾的值小,说明对于后面的数来说,与当前位置sum的差肯定大于队尾的数,所以删除队尾的数,直到队尾的数小于当前的值或者队列空。因为队列长度不能超过k,所以从队头开始删,直到队头的数的位置大于等于当前的位置-k。当前的最值就是当前位置的值减去队头的值,然后维护总的值即可。
 
/*
 * Author:  sweat123
 * Created Time:  2016/7/11 21:46:18
 * File Name: main.cpp
 */
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = 200010;
deque<int>q;
int a[MAXN],n,k,sum[MAXN],cnt;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        q.clear();
        scanf("%d%d",&n,&k);
        for(int i = 1; i <= n; i++){
            scanf("%d",&a[i]);
        }   
        for(int i = 1; i < k; i++){
            a[i+n] = a[i];    
        }
        sum[0] = 0;
        for(int i = 1; i < n + k; i++){
            sum[i] = sum[i-1] + a[i];
        }
        int ans = -INF,l,r;
        for(int i = 1; i < n + k; i++){
            while(!q.empty() && sum[q.back()] > sum[i-1]){
                q.pop_back();
            }
            while(!q.empty() && q.front() < (i - k)){
                q.pop_front();   
            }
            q.push_back(i-1);
            int val = sum[i] - sum[q.front()];
            if(val > ans){
                ans = val;
                l = q.front();
                r = i;   
            }
        }
        if(r > n) r %= n;
        printf("%d %d %d\n",ans,l+1,r);
    }
    return 0;
}

 

 
 

hdu3415 单调队列

标签:

原文地址:http://www.cnblogs.com/sweat123/p/5661855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!