Given a sequence of K integers { N1​​, N2​​, ..., NK​​ }. A continuous subsequence is defined to be { Ni​​, Ni+1​​, ..., Nj​​ } where 1ijK. 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 #include <stdio.h>
 2 int MaxSubSum(int a[],int n);
 3 int FirstI,EndI;
 4 int main()
 5 {
 6     int n,a[10005],MaxSum;
 7     while(scanf("%d",&n)!=EOF)
 8     {
 9         for(int i = 0;i < n;i++)
10         scanf("%d",&a[i]);
11         MaxSum = MaxSubSum(a,n);
12         if(MaxSum != -1)
13             printf("%d %d %d\n",MaxSum,a[FirstI],a[EndI]);    
14         else
15             printf("0 %d %d\n",a[FirstI],a[EndI]);    
16     }
17     return 0;
18 }
19 int MaxSubSum(int a[],int n)
20 {
21     int ThisSum = 0,MaxSum= -1;
22     int firsti=-1;
23     FirstI = 0;EndI = n-1;
24     for(int i = 0;i < n;i++){
25         ThisSum += a[i];
26         if(MaxSum < ThisSum){
27             MaxSum = ThisSum;
28             EndI = i;
29             FirstI = firsti + 1;
30         }
31         else if(ThisSum < 0)
32         {
33             ThisSum = 0;
34             firsti = i;
35         }
36     }
37     return MaxSum;
38 }

 忘掉以前写过了,又写一遍。。。然而总体思路好像差不多 但是这二次有一项不通过。。。记录下(已改正)上下两个想法一模一样,不愧是亲生的。。。

 1 //并列和对应相同i,不同j,即尾是0   该项不通过 
 2 #include <stdio.h>
 3 int left = 0,right = 0,right_left = 0;
 4 int MaxSubseqSum4(int a[],int size)
 5 {
 6     int ThisSum = 0,MaxSum = -1;        //MaxSum = -1是为了只有负数和 0时,记录第一个0位置 
 7      for(int i = 0;i < size;i++){
 8          ThisSum += a[i];
 9         if(MaxSum < ThisSum) {
10             MaxSum = ThisSum;
11             right = i;
12             right_left = left;        //因为l之后ThisSum < 0时left会变 把此时right对应的left记录下来 
13         }
14         else if(ThisSum < 0) {
15             ThisSum = 0;
16             left = left + 1;      //原为left++ 此处出错  left+1为了记录最大子列的第一个的位置
17         }  
18     }
19     return MaxSum;
20 }
21 int main()
22 {
23     int n,a[100005];
24 
25     scanf("%d",&n); 
26     for(int i = 0;i < n; i++) {
27         scanf("%d",&a[i]);
28     }
29     int MaxSum = MaxSubseqSum4(a,n);
30     
31     if(MaxSum == -1) {        //全是负数        //原为left == n 上面错了 这里也错了
32         printf("0 %d %d\n", a[0], a[n-1]);
33     } else {
34         printf("%d %d %d\n", MaxSum, a[right_left], a[right]);
35     }
36     return 0;
37 }

 

posted on 2015-09-29 16:53  kuotian  阅读(488)  评论(0编辑  收藏  举报