zoukankan      html  css  js  c++  java
  • DP专题训练之HDU 1231 最大连续子序列

    Description

    给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ...,
    Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,
    例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和
    为20。
    在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该
    子序列的第一个和最后一个元素。
     

    Input

    测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
     

    Output

    对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元
    素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
     

    Sample Input

    6
    -2 11 -4 13 -5 -2
    10
    -10 1 2 3 4 -5 -23 3 7 -21
    6
    5 -8 3 2 5 0
    1
    10
    3
    -1 -5 -2
    3
    -1 0 -2 0
     

    Sample Output

    20 11 13
    10 1 4
    10 3 5
    10 10 10
    0 -1 -2
    0 0 0

    Hint

    Hint  Huge input, scanf is recommended.
    

    直接遍历然后不断更新下标和最大值~

    //Asimple
    //#include <bits/stdc++.h>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <cctype>
    #include <cstdlib>
    #include <stack>
    #include <cmath>
    #include <set>
    #include <map>
    #include <string>
    #include <queue>
    #include <limits.h>
    #include <time.h>
    #define INF 0xfffffff
    #define mod 1000000
    #define swap(a,b,t) t = a, a = b, b = t
    #define CLS(a, v) memset(a, v, sizeof(a))
    #define debug(a)  cout << #a << " = "  << a <<endl
    #define abs(x) x<0?-x:x
    #define srd(a) scanf("%d", &a)
    #define src(a) scanf("%c", &a)
    #define srs(a) scanf("%s", a)
    #define srdd(a,b) scanf("%d %d",&a, &b)
    #define srddd(a,b,c) scanf("%d %d %d",&a, &b, &c)
    #define prd(a) printf("%d
    ", a)
    #define prdd(a,b) printf("%d %d
    ",a, b)
    #define prs(a) printf("%s
    ", a)
    #define prc(a) printf("%c", a)
    using namespace std;
    typedef long long ll;
    const int maxn = 10001;
    int n, m, num, T, k, len, ans, sum;
    int edx, edy, stx, sty;
    int dp[1000001];
    int a[maxn];void input() {
        while( ~srd(n) && n ) {
            sum = 0;
            for(int i=0; i<n; i++) {
                srd(a[i]);
            }
            int inds = 0;
            int inde = 0, t = 0;
            int Maxsum = -INF;
            int sum = 0;
            for(int i=0; i<n; i++) {
                if( sum < 0 ) {
                    sum = a[i];
                    t = i;
                } else sum += a[i];
                if( sum > Maxsum ) {
                    Maxsum = sum;
                    inds = t;
                    inde = i;
                }
            }
            if( Maxsum < 0 ) {
                printf("0 %d %d
    ", a[0], a[n-1]);
            } else {
                printf("%d %d %d
    ", Maxsum, a[inds], a[inde]);
            } 
        }
    }
    
    int main(){
        input();
        return 0;
    }


    加一个一模一样的题~~就只是不会出现全为负的情况~

    http://acm.hdu.edu.cn/showproblem.php?pid=1003

    //Asimple
    //#include <bits/stdc++.h>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <cctype>
    #include <cstdlib>
    #include <stack>
    #include <cmath>
    #include <set>
    #include <map>
    #include <string>
    #include <queue>
    #include <limits.h>
    #include <time.h>
    #define INF 0xfffffff
    #define mod 1000000
    #define swap(a,b,t) t = a, a = b, b = t
    #define CLS(a, v) memset(a, v, sizeof(a))
    #define debug(a)  cout << #a << " = "  << a <<endl
    #define abs(x) x<0?-x:x
    #define srd(a) scanf("%d", &a)
    #define src(a) scanf("%c", &a)
    #define srs(a) scanf("%s", a)
    #define srdd(a,b) scanf("%d %d",&a, &b)
    #define srddd(a,b,c) scanf("%d %d %d",&a, &b, &c)
    #define prd(a) printf("%d
    ", a)
    #define prdd(a,b) printf("%d %d
    ",a, b)
    #define prs(a) printf("%s
    ", a)
    #define prc(a) printf("%c", a)
    using namespace std;
    typedef long long ll;
    const int maxn = 100001;
    int n, m, num, T, k, len, ans, sum;
    int edx, edy, stx, sty;
    int a[maxn];
    
    void input() {
        srd(T);
        for(int k=1; k<=T; k++) {
            srd(n);
            for(int i=1; i<=n; i++) {
                srd(a[i]);
            }
            int inds = 1;
            int inde = 1, t = 1;
            int Maxsum = -INF;
            int sum = 0;
            for(int i=1; i<=n; i++) {
                if( sum < 0 ) {
                    sum = a[i];
                    t = i;
                } else sum += a[i];
                if( sum > Maxsum ) {
                    Maxsum = sum;
                    inds = t;
                    inde = i;
                }
            }
            printf("Case %d:
    ", k);
            printf("%d %d %d
    ", Maxsum, inds, inde);
            if( k < T ) {
                printf("
    ");
            }
        }
    }
    
    int main(){
        input();
        return 0;
    }
    低调做人,高调做事。
  • 相关阅读:
    MySQL中IS NULL、IS NOT NULL、!=不能用索引?胡扯!
    市值TOP10,人类进化及中美坐标
    倒序切片
    对list进行切片
    Python之定义可变参数
    Python之递归函数
    Python之“可变”的tuple
    Python之创建单元素tuple
    Python中Unicode字符串
    Pycharm配置autopep8让Python代码更符合pep8规范
  • 原文地址:https://www.cnblogs.com/Asimple/p/6229889.html
Copyright © 2011-2022 走看看