• HDU


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

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define lowbit(x) (x & (-x))
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 10000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int a[MAXN];
    int main(){
        int K;
        while(scanf("%d", &K) == 1){
            if(K == 0) return 0;
            for(int i = 0; i < K; ++i){
                scanf("%d", &a[i]);
            }
            int st, ed, sum, ans, tmpst;
            st = ed = sum = tmpst = 0;
            ans = -1;
            bool ok = false;
            for(int i = 0; i < K; ++i){
                if(sum < 0){
                    sum = a[i];
                    tmpst = i;
                }
                else{
                    sum += a[i];
                }
                if(sum > ans){
                    ok = true;
                    ans = sum;
                    st = tmpst;
                    ed = i;
                }
            }
            if(!ok){
                printf("0 %d %d
    ", a[0], a[K - 1]);
            }
            else{
                printf("%d %d %d
    ", ans, a[st], a[ed]);
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    在IDEA中使用maven
    使用IDEA创建JavaWeb项目 部署本地tomcat并运行
    Java
    c++
    Vue学习
    svn 小程序地址
    SVN 上传代码
    eclipse插件 --js
    https抓包
    eclipse 断点位置发生莫名其妙的位移
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7340668.html
Copyright © 2020-2023  润新知