1007. Maximum Subsequence Sum (25)

来源:互联网 发布:camera软件下载 编辑:程序博客网 时间:2024/06/09 19:46

1007. Maximum Subsequence Sum (25)

目录

  • Maximum Subsequence Sum 25
    • 目录
    • 原题
    • 思路
    • 代码

原题

Given a sequence of K integersN1,N2,...,NK. A continuous subsequence is defined to be Ni,Ni+1,...,Nj where 1<=i<=j<=K. The Maximum 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

思路

用动态规划解决此问题。

1. 设置数组dp,dp[i]代表以第i个元素结尾的最大子序列和。设置数组start,start[i]代表以第i个元素结尾的最大子序列的起始位置。设置int max = INT_MIN, max_index = k - 1。设置flag=true,序列中的元素是否全部为零。2. 读入k。3. 读入第一个数字array[0]。对于第一个数字,dp[i] = array[0],start[i] = 0。如果array[0]>=0,那么max = array[0], max_index = 0,flag=false。4. 循环k-1次。    1. 读入第i个数字array[i]。    2. 如果array[i]大于等于零,flag=false。    3. 如果dp[i-1] < 0,start[i] = i, dp[i] = temp。    4. 否则,start[i] = start[i-1],dp[i]=dp[i-1]+temp。    5.  如果dp[i] > max,那么max = dp[i], max_index = i。5.  输出max,如果flag == true,输出array[0],array[k-1],否则输出array[start[max_index]],array[max_index]。

代码

#include <iostream>#include <cstdio>#include <climits>int main(void){    setvbuf(stdin, new char[1 << 20], _IOFBF, 1 << 20);    setvbuf(stdout, new char[1 << 20], _IOFBF, 1 << 20);    int k;    scanf("%d", &k);    int *dp = new int[k], *start = new int[k], max = INT_MIN, max_index = k - 1;    int *array = new int[k];    scanf("%d", array);    bool flag = true;    if (array[0] >= 0) {        max = array[0];        max_index = 0;        flag = false;    }    dp[0] = array[0];    start[0] = 0;    for (int i = 1; i < k; i++) {        scanf("%d", array + i);        if (array[i] >= 0) {            flag = false;        }        if (dp[i - 1] < 0) {            dp[i] = array[i];            start[i] = i;        }        else {            dp[i] = dp[i - 1] + array[i];            start[i] = start[i - 1];        }        if (max < dp[i]) {            max = dp[i];            max_index = i;        }    }    if (flag) {        printf("%d %d %d\n", 0, array[0], array[k - 1]);    }    else {        printf("%d %d %d\n", max, array[start[max_index]], array[max_index]);    }    delete[] array;    delete[] start;    return 0;}
0 0
原创粉丝点击