• PAT A1007 Maximum Subsequence Sum


    1007 Maximum Subsequence Sum

    分数 25

    作者 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. 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:

    1. 10
    2. -10 1 2 3 4 -5 -23 3 7 -21

    Sample Output:

    10 1 4

    两种方法,一种在线处理,即边输入边处理,另一种动态规划,但是得额外增加一个数组来记录使得当前数取得最大值的前驱位置。

    在线处理:

    1. #include <iostream>
    2. #include <cstring>
    3. #include <algorithm>
    4. using namespace std;
    5. const int INF = 1e9 ,maxn = 1e5;
    6. int a[maxn];
    7. int main()
    8. {
    9. int n;
    10. cin >> n;
    11. for(int i=0; i<n; ++i)
    12. cin >> a[i];
    13. int maxsum = -INF,l = 0, r = n-1;
    14. int tempsum = 0,templ = 0,tempr = 0;
    15. for(int i=0;i<n;++i)
    16. {
    17. if(tempsum + a[i] < 0)
    18. {
    19. tempsum = 0;
    20. templ = i + 1;
    21. }
    22. else
    23. {
    24. tempsum += a[i];
    25. tempr = i;
    26. }
    27. if(tempsum > maxsum && templ <= tempr)
    28. {
    29. maxsum = tempsum;
    30. l = templ;
    31. r = tempr;
    32. }
    33. }
    34. if(maxsum < 0)
    35. maxsum = 0;
    36. cout << maxsum << ' ' << a[l] << ' ' << a[r] << endl;
    37. return 0;
    38. }

    动态规划

    1. #include <iostream>
    2. #include <cstring>
    3. #include <algorithm>
    4. using namespace std;
    5. const int maxn = 1e4+10;
    6. int a[maxn], dp[maxn],pre[maxn];
    7. int main()
    8. {
    9. int n;
    10. cin >> n;
    11. for(int i=1;i<=n;++i)
    12. {
    13. cin >> a[i];
    14. pre[i] = i;
    15. }
    16. for(int i=1;i<=n;++i)
    17. {
    18. if(dp[i-1] + a[i] > a[i])
    19. {
    20. dp[i] = dp[i-1] + a[i];
    21. pre[i] = i-1;
    22. }
    23. else
    24. {
    25. dp[i] = a[i];
    26. pre[i] = i;
    27. }
    28. }
    29. int l = 1, r= n, maxsum = -maxn*maxn;
    30. for(int i=1;i<=n;++i)
    31. {
    32. if(dp[i] > maxsum)
    33. {
    34. maxsum = dp[i];
    35. r = i;
    36. }
    37. }
    38. l = r;
    39. while(pre[l] != l)
    40. l = pre[l];
    41. if(maxsum < 0)
    42. {
    43. maxsum = 0;
    44. l = 1;
    45. r= n;
    46. }
    47. cout << maxsum << ' ' << a[l] << ' ' << a[r] << endl;
    48. return 0;
    49. }

  • 相关阅读:
    解决springboot启动jar包时加载了jar包内的配置文件
    这可能是最全的 SpringBoot3 新版本变化了
    Python实现给图片加水印功能
    分账系统二清解决方案如何助力电商平台合规经营?
    es安装方式
    线性回归模型求解
    阿里巴巴Java工程师:怎么才算掌握了JDK中的线程池?
    纺织ERP系统哪家的比较好?适用的纺织ERP软件有哪些
    Net 高级调试之一:开始认识一些调试工具
    前端面试宝典总结1-HTML
  • 原文地址:https://blog.csdn.net/qq_51825761/article/details/127835155