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

Max Sum of Max-K-sub-sequence

时间:2016-02-21 18:23:01      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

Max Sum of Max-K-sub-sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6844    Accepted Submission(s): 2518

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
 题解:
让找长度小于等于k的最长字串合;
暴力超时;
超时代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x) scanf("%lf",&x)
#define P_ printf(" ")
typedef long long LL;
const int MAXN=100010;
int m[MAXN];
int main(){
    int T,N,K;
    SI(T);
    while(T--){
        SI(N);SI(K);
        for(int i=1;i<=N;i++)SI(m[i]);
        int cur=0,ans=-INF,l,r;
        for(int i=1;i+K-1<=N;i++){
            cur=0;
            for(int j=0;j<K;j++){
                cur+=m[i+j];
                if(cur>ans){
                    ans=cur;
                    l=i;r=i+j;
                }
            }
        }
        printf("%d %d %d\n",ans,l,r);
    }
    return 0;
}

 

Max Sum of Max-K-sub-sequence

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/5205202.html

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