zoukankan      html  css  js  c++  java
  • Maximum Subsequence Sum 【DP】

    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≤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

    思路
    用DP的方法 动态更新答案 每次对每一个数求和 当和小于 0 的时候 就重置为0 然后 sum > ans 的时候 就更新

    然后 有一个难点 就是 要输出 该子串的 首尾 元素

    一共有几种情况

    0.最大和序列中有负数
    1.并列和对应i相同但是不同j,即 尾部有0
    2.1个正数
    3.全是负数
    4.负数和0
    5.最大和前面有一段0
    6.最大N

    我们只需要标记一下 起点就可以了
    然后更新答案的那个 就是尾部

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    typedef pair<string, int> psi;
    typedef pair<string, string> pss;
    
    const double PI = 3.14159265358979323846264338327;
    const double E = exp(1);
    const double eps = 1e-6;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e3 + 5;
    const int MOD = 1e9 + 7;
    
    int arr[maxn];
    
    int main()
    {
        int t;
        cin >> t;
        for (int l = 1; l <= t; l++)
        {
            int n, k;
            scanf("%d%d", &n, &k);
            ll ans = 0;
            for (int i = 0; i < n; i++)
                scanf("%d", &arr[i]);
            sort(arr, arr + n);
            int len = n - k + 1;
            for (int i = 0; i < len; i++)
                ans -= arr[i];
            for (int i = n - 1, j = 0; j < len; i--, j++)
                ans += arr[i];
            printf("Case #%d: %d
    ", l, ans);
        }
    }
  • 相关阅读:
    Fragment 总结
    Varnish缓存服务器的搭建配置手册
    关于页面缓存服务器的研究报告
    基于Html5的移动端开发框架的研究
    C#的Process类的一些用法
    c#中进程的使用
    C#反射(转载)
    进制的转换 以及十进制转换成x进制的代码
    算法及其复杂度
    cocos总结
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433216.html
Copyright © 2011-2022 走看看