【DP-最大子串和】PAT1007. Maximum Subsequence Sum

1007. Maximum Subsequence Sum (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } 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



  1 /*
  2 Sample Input:
  3 10
  4 -10 1 2 3 4 -5 -23 3 7 -21
  5 Sample Output:
  6 10 1 4
  7 分析:
  8     首先,既然要记录最大和序列的开始位置、结束位置、最大和以及当前序列的开始位置、结束位置、当前和(不包括当前数),
  9                         则分别定义ansst,ansend,ansmax,                    curst,curend,cursum;
 10                         初始化:当前的和=0;
 11           (刚开始不知道这几个变量初始化为多少,先看测试数据;)
 12     接下来,看策略:
 13       1、-10
 14             当前和=0,当前数=-10,因为当前和+当前数=-10<0【4】,显然不可取,因此,我们把当前和=0,当前的开始位置置于下一个下标;
 15       2、1 2 3 4
 16              这些数都是正数【1】,因此,肯定是可取的,这时,就要记录当前结束位置(因为开始位置在第一个数-10的时候已经记录过了,所以不必记录)
 17              当前和累加;因为当前和>最大和,因此,最大和序列的开始,结束位置以及最大和都要更新;
 18       3、-5
 19              负数,显然,这里我们不能去更新最大和序列的一些属性(因为无法判断,但是之前的1,2,3,4序列已经被保存),
 20              注意:1+2+3+4(当前最大和)+(-5)>0【3】,因此,
 21              所以,这里我们要更新的是当前序列的一些属性,结束位置要更新,当前和肯定也要记录(因为可能后面有一个超大正数);
 22       4、-23
 23              此时,cursum=1+2+3+4-5=5,5+(-23)<0【4】,因此,如果加入-23,显然没有之前的大,
 24              而且如果后面有个超大正数,也不可能加上前面的序列(因为他的和是个负数,起了一个削减的作用),因此采用和步骤1相同的策略;
 25       5、3
 26              此时,ansmax=10,cursum=0,curst=7(下标),0+7<10【2】,因此,还无法“动摇”最大和序列,但是,我们已经开始记录结束位置了;
 27       6、7
 28              cursum=3+7=10,10<=10,按照题意,不更新最大和序列,因此,还是选择【2】策略;
 29       7、-21
 30              cursum=10-21<0【4】
 31      经过以上的分析,我们就能写出相应的代码,这时,还有一个问题,就是变量初始化的问题:
 32               如果有这么一组测试数据:
 33               4
 34               -1 -2 0 -1
 35               按照题意显然应该输出:
 36               0 0 0
 37               因此,在当前数为0时,我们应该去更新最大和序列,也就是当前和>最大和【1】,故,ansmax设为负数
 38               再看一组:
 39               2
 40               100 10
 41               显然刚开始就要更新最大和序列,而最大和序列的属性值来源于当前和序列,因此,当前和序列的所有参数值必须全部进行初始化为0;
 42     若当前数为非负数,且加上当前和>最大和,则:1、当前和+=当前数;2、更新最大和序列;                 【1】
 43                     且加上当前和<=最大和,则:1、当前和+=当前数;2、更新当前序列的结束位置;          【2】
 44     若当前数为负数,且当前和+当前数>0,则:1、当前和+=当前数;2、更新当前序列的结束位置;             【3】                               
 45                     且当前和+当前数<0,则:1、当前和=0;2、更新当前序列的开始位置为下一个下标         【4】
 46     注:这里为什么负数就要和0进行比较,因为加上一个负数,和肯定是减小的,也就是说之前的最大和序列的属性值已经被保存。
 47 */
 48 
 49 #include<iostream>
 50 #include<stdio.h>
 51 using namespace std;
 52 
 53 #define INF 0x7fffffff
 54 
 55 int max(int a,int b)
 56 {
 57     return a>b?a:b;
 58 }
 59 
 60 int data[10002];
 61 
 62 int main()
 63 {
 64     int i;
 65     int n;
 66     int curst,curend,cursum;
 67     int ansst,ansend,ansmax;
 68     cursum=curst=curend=0;
 69     ansmax=-1;
 70     bool flag=true;
 71     scanf("%d",&n);
 72     for(i=0;i<n;++i)
 73     {
 74         scanf("%d",&data[i]);
 75         if(data[i]>=0)
 76         {
 77             flag=false;
 78         }
 79     }
 80     if(flag)                     //全部都是负数
 81     {
 82         printf("0 %d %d
",data[0],data[n-1]);
 83         return 0;
 84     }
 85     for(i=0;i<n;++i)
 86     {
 87         if(data[i]>=0)
 88         {
 89             if(cursum+data[i]>ansmax)
 90             {
 91                 ansst=curst;
 92                 ansend=curend=i;
 93                 ansmax=cursum+data[i];
 94                 cursum+=data[i];
 95             }        
 96             else
 97             {
 98                 cursum+=data[i];
 99                 curend=i;
100             }
101         }
102         else
103         {
104             if(cursum+data[i]>0)
105             {
106                 cursum+=data[i];
107                 curend=i;
108             }
109             else
110             {
111                 curst=i+1;
112                 cursum=0;
113             }
114         }
115     }
116     printf("%d %d %d
",ansmax,data[ansst],data[ansend]);
117     return 0;
118 }








            If you have any questions about this article, welcome to leave a message on the message board.



Brad(Bowen) Xu
E-Mail : maxxbw1992@gmail.com


原文地址:https://www.cnblogs.com/XBWer/p/3632435.html