zoukankan      html  css  js  c++  java
  • PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum

      Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1. 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 (≤). 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

    题目解析

      本题第一行给出数组长度k,下一行给出k个整数,要求计算出最大连续子串和,并输出最大连续字串的首位与末位,如果整个字符串全为负数,则输出0并输出整个字符串的首位与末位。

      用数组arr记录给出的数组,最大连续子串和,果断dp。用数组dp[i]记录当字符串以第i位结尾时的最大连续子串和。当i = 0时以首位为结尾的最大连续子串和为首位arr[ i ]的值。

    之后的值只会有两种情况:

      1)以当前位i为结尾时最大连续子串只有其本身arr[ i ]一个元素;

      2)以当前位i为结尾时最大连续子串有多个元素,即dp[ i - 1] + arr[ i ];

      可以得到状态转移方程dp[ i ] = max(arr[ i ], dp[ i - 1] + arr[ i ]);

      至于最大连续子串的首尾两位我们可以在计算dp数组时一并计算,以bg[ i ]记录dp[ i ]对应的最大连续子串的首位,以ed[ i ]记录dp[ i ]对应的最大连续子串的末尾。

    注意:

           若不对整个字符串全为负数的情况做出处理, 测试点4会错误;

      若对整个字符串全为负数的情况做出处理了,但将字符串存在0的情况也视为全为负数处理,测试点5会错误。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int MAX = 1e4+10;
     4 int arr[MAX];
     5 int dp[MAX], bg[MAX], ed[MAX];
     6 int main()
     7 {
     8     int k;
     9     scanf("%d", &k);    //输入数组长度k
    10     bool flag = false;  //flag记录数组是否全为负数
    11     for(int i = 0; i < k; i++){ //输入数组
    12         scanf("%d", &arr[i]);
    13         if(arr[i] >= 0) //只要存在正数或0则将flag标记位true
    14             flag = true;
    15     }
    16     dp[0] = arr[0]; //以首位为结尾的最大连续连续子串和为首位arr[i]的值
    17     int maxSum = arr[0], index = 0;
    18     //maxSum记录最大连续子串和
    19     //index记录最大连续子串的末位下标
    20     for(int i = 1; i < k; i++){
    21         if(dp[i - 1] + arr[i] >= arr[i]){   //情况2
    22             dp[i] = dp[i - 1] + arr[i];
    23             bg[i] = bg[i - 1];  //记录当前最大连续子串首位下标
    24             ed[i] = i;  //记录最大连续子串末位下标
    25         }else{  //情况1
    26             dp[i] = arr[i];
    27             bg[i] = ed[i] = i;
    28             //最大连续子串首尾下标都为i
    29         }
    30         if(dp[i] > maxSum){
    31             maxSum = dp[i];
    32             index = i;
    33         }
    34     }
    35     if(flag)
    36         printf("%d %d %d
    ", maxSum, arr[bg[index]], arr[ed[index]]);
    37     else
    38         printf("0 %d %d
    ", arr[0], arr[k - 1]);
    39     return 0;
    40 }

      当然本题数据量最大只有1e4,暴力的O(n^2)并不会超时,所以本题暴力依然没有问题。

    暴力过一切:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int MAX = 1e4+10;
     4 int arr[MAX], sum[MAX];
     5 int main(){
     6     int k;
     7     scanf("%d", &k);
     8     int temp = 0;
     9     for(int i = 0; i < k; i++){
    10         scanf("%d", &arr[i]);
    11         temp += arr[i];
    12         sum[i] = temp;
    13     }
    14     int maxSum = -1, bg = 0, ed = k - 1;
    15     for(int i = 0; i < k; i++){
    16         for(int j = i; j < k; j++){
    17             if(sum[j] - sum[i] + arr[i] > maxSum){
    18                 maxSum = sum[j] - sum[i] + arr[i];
    19                 bg = i;
    20                 ed = j;
    21             }
    22         }
    23     }
    24     if(maxSum < 0)
    25         maxSum = 0;
    26     printf("%d %d %d
    ", maxSum, arr[bg], arr[ed]);
    27     return 0;
    28 }
  • 相关阅读:
    软件測试系列之软件測试过程模型(四)
    实例具体解释Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(二)
    Hadoop-2.2.0中文文档—— MapReduce 下一代
    const vs readonly
    oracle-asm
    Javascript 笔记与总结(2-15)结构、样式、行为分离
    [Swift]LeetCode455. 分发饼干 | Assign Cookies
    [Swift]LeetCode453. 最小移动次数使数组元素相等 | Minimum Moves to Equal Array Elements
    [Swift]LeetCode448. 找到所有数组中消失的数字 | Find All Numbers Disappeared in an Array
    [Swift]LeetCode443. 压缩字符串 | String Compression
  • 原文地址:https://www.cnblogs.com/suvvm/p/10671900.html
Copyright © 2011-2022 走看看