• 1007_Maximum Subsequence Sum (25分)[动态规划/最大和子序列]


    题意: 输出子序列的最大和以及子序列的首尾元素.如果存在多个最大和子序列取位置最靠左的那个.如果序列元素全<0,和为0,输出给定序列的首尾元素.

     1 #include<iostream>
     2 #include<vector>
     3 #include<queue>
     4 #include<map>
     5 #include<set>
     6 #include<cmath>
     7 #include<cstdio>
     8 #include<cstdlib>
     9 #include<cstring>
    10 #include<algorithm>
    11 using namespace std;
    12 
    13 const int maxLen = 10000 + 10;
    14 int val[maxLen], dp[maxLen], front[maxLen], N, maxSum, maxR;
    15 
    16 int main()
    17 {
    18     cin >> N;
    19     memset(dp, 0, sizeof(dp));
    20     memset(val, 0, sizeof(val));
    21     memset(front, 0, sizeof(front));
    22     for (int i = 0; i < N; i++) {
    23         cin >> val[i];
    24         dp[i] = val[i];
    25         front[i] = i;
    26     }
    27     maxSum = dp[0]; maxR = 0; //初始化
    28     for (int i = 1; i < N; i++) {
    29         if (dp[i - 1] >= 0) { //动态规划
    30             dp[i] = dp[i] + dp[i - 1];
    31             front[i] = front[i - 1];
    32         }
    33         if (dp[i] > maxSum) { //记录:最靠左的最大子序列
    34             maxSum = dp[i];
    35             maxR = i;
    36         }
    37     }
    38     if (maxSum < 0) {
    39         cout << 0 << " " << val[0] << " " << val[N - 1] << endl;
    40     }
    41     else {
    42         cout << maxSum << " " << val[front[maxR]] << " " << val[maxR] << endl;
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    一起ORA-00028案例的处理过程
    Preferences偏好设置
    Snap Settings对齐设置
    Graphics Emulation图形模拟
    Network Emulation(网格模拟)
    Selection
    Edit编辑
    Build Settings 构建设置
    Player Settings-PC
    Build Settings发布设置
  • 原文地址:https://www.cnblogs.com/NiBosS/p/12102484.html
Copyright © 2020-2023  润新知