[DP-최대 하위 꼬치와] PAT1007.Maximum Subsequence Sum

15810 단어 sequence
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 }

 


좋은 웹페이지 즐겨찾기