zoukankan      html  css  js  c++  java
  • HDUOJ----1003 Max Sum

    Max Sum

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 113031    Accepted Submission(s): 26130


    Problem Description
    Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
     
    Input
    The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
     
    Output
    For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
     
    Sample Input
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
     
    Sample Output
    Case 1: 14 1 4
     
     Case 2: 7 1 6
     
    Author
    Ignatius.L
    动态规划....求最大子段和;
    代吗:
     1 #include<iostream>
     2 #include<vector>
     3 using namespace std;
     4 int main()
     5 {
     6     int t,n,i,k,st,en,maxn,sum,flag=1;
     7     cin>>t;
     8     while(t--)
     9     {
    10         cin>>n;
    11         vector<int>arr(n);
    12         for(i=0;i<n;i++)
    13             cin>>arr[i];
    14           st=en=sum=0;
    15         for(i=0,k=0;i<n;i++)
    16          {
    17             sum+=arr[i];
    18             if(i==0||sum>maxn)
    19            {
    20                 maxn=sum;
    21                 en=i;
    22                 st=k;
    23            } 
    24           if(sum<0)
    25            {
    26              sum=0;
    27              k=i+1;
    28            }
    29          }
    30          cout<<"Case "<<flag++<<":"<<endl;
    31          cout<<maxn<<" "<<st+1<<" "<<en+1<<endl;
    32          if(t!=0)cout<<endl;
    33     }
    34     return 0;
    35 }
    View Code
  • 相关阅读:
    【题解】UOJ61. 【UR #5】怎样更有力气
    【题解】Kruskal重构树——[NOI2018] 归程
    图论补档——KM算法+稳定婚姻问题
    NOIP2018 提高组题解
    杂物
    朱刘算法学习笔记
    文化课の疑难杂症
    FHQ简要笔记
    题解 AT3877 【[ARC089C] GraphXY】
    CSP-S 2020 退役记
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3237541.html
Copyright © 2011-2022 走看看