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): 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;
}
时间: 2024-10-13 23:14:20

Max Sum of Max-K-sub-sequence的相关文章

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

K - Max Sum Plus Plus

K - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more diffi

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 such that its sum is no larger than k. Example: Given matrix = [ [1, 0, 1], [0, -2, 3] ] k = 2 The answer is 2. Because the sum of rectangle [[0, 1], [

【leetcode】363. 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 such that its sum is no larger than k. 解题思路: 根据题意,寻找二维数组中所有可以组成的矩形中面积不超过k的最大值,所以必须要求出可能组成的矩形的面积并与k比较求出最终结果.这里为了最终不超时,可以在一下方面进行优化: 1.设置一个数组比较当前列(或

[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 such that its sum is no larger than k. Example: Given matrix = [ [1, 0, 1], [0, -2, 3] ] k = 2 The answer is 2. Because the sum of rectangle [[0, 1], [

363 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 such that its sum is no larger than k.Example:Given matrix = [  [1,  0, 1],  [0, -2, 3]]k = 2The answer is 2. Because the sum of rectangle [[0, 1], [-2

HDU 1003 Max Sum【动态规划求最大子序列和详解 】

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 250714    Accepted Submission(s): 59365 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su

HDU 1024 Max Sum Plus Plus

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21926    Accepted Submission(s): 7342 Problem Description Now I think you ha