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

PAT 1007

时间:2015-12-09 19:32:59      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

1007. Maximum Subsequence Sum (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. TheMaximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4

解析:需要注意两点,第一点就是子序列最大值为0的情况,第二点就是如果使用DP可能不是最优解法,有可能会内存超出,这里首先贴上的是DP的解法,没有完全AC,最后一组报出内存超出,后面的代码使用了更加巧妙的方法,在O(n)的储存下解决了问题。
Code:
/*************************************************************************
    > File Name: 1007.cpp
    > Author: 
    > Mail: 
    > Created Time: 2015年12月08日 星期二 21时54分51秒
 ************************************************************************/

#include<iostream>
#include<vector>
using namespace std;

int main(){
    int k;
    cin>>k;
    int n = k;

    vector<int> array;
    int tmp;
    while(k--){
        cin>>tmp;
        array.push_back(tmp);
    }

    vector<vector<int> > sums(n, vector<int>(n, 0));
    for(int i=0; i<n; i++){
        sums[i][i] = array[i];
    }

    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            sums[i][j] = sums[i][j-1] + array[j];
        }
    }

    bool flag = false;
    int max = 0;
    int start = 0;
    int end = 0;
    for(int i=0; i<n; i++){
        for(int j=i; j<n; j++){
            if(sums[i][j] == 0 && max == 0){
                max = 0;
                start = i;
                end = j;
                flag = true;
            }
            if(sums[i][j] > max){
                max = sums[i][j];
                start = i;
                end = j;
                flag = false;
            }
        }
    }

    if(max == 0 && !flag){
        cout<<0<<" "<<array[0]<<" "<<array[n-1];
    }
    else{
        cout<<max<<" "<<array[start]<<" "<<array[end];
    }
    return 0;
}

接下来借鉴了 http://www.2cto.com/kf/201308/238675.html上的方法,该方法遍历数组的过程中记录最大字串的start和end,以及sum的值,当sum值小于0的时候,就从现在的位置重新选择最大字串。

/*************************************************************************
    > File Name: 1007_m.cpp
    > Author: 
    > Mail: 
    > Created Time: 2015年12月08日 星期二 22时20分24秒
 ************************************************************************/

#include<iostream>
using namespace std;

int main(){
    int k;
    cin>>k;
    int n = k;

    int *array = new int[n];
    int tmp;
    int index=0;
    while(k--){
        cin>>tmp;
        array[index] = tmp;
        index++;
    }

    int sum = 0;
    int tempSum = 0;
    int begin = 0;
    int end = 0;
    int tempBegin = 0;
    int tempEnd = 0;
    end = n-1;
    for(int i=0; i<n; i++){
        if(tempSum >= 0){
            tempSum += array[i];
            tempEnd = i;
        }
        else{
            tempSum = 0;
            tempSum += array[i];
            tempBegin = i;
            tempEnd = i;
        }

        if(tempSum > sum || (tempSum == 0 && end == n-1)){
            sum = tempSum;
            begin = tempBegin;
            end = tempEnd;
        }
    }

    cout<<sum<<" "<<array[begin]<<" "<<array[end]<<endl;

    return 0;
}

 

 

PAT 1007

标签:

原文地址:http://www.cnblogs.com/RookieCoder/p/5031259.html

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