poj2593

Max Sequence


|code--------------------------------------------------------------------------------------------------+

|001| /***************************************************************\                               

|002|  *Author: 小呼                                                                                

|003|  *Created Time: Tue 06 Jul 2010 02:49:20 PM CST                                                 

|004|  *File Name: a.cpp                                                                              

|005|  *Description:动态规划。Max_L[i]表示A[1...i]中最大的连续和,同理               

|006|  *Max_R[i]是指A[i...n]中最大的连续和                                                   

|007| \***************************************************************/                               

|008| //*======================*Head File*======================*\\                                   

|009|                                                                                                 

|010| #include<iostream>                                                                              

|011| #include<stdio.h>                                                                               

|012| #include<stdlib.h>                                                                              

|013| #include<string.h>                                                                              

|014| /*----------------------*Global Variable*----------------------*/                               

|015|                                                                                                 

|016| //*=====================*Main Program*=====================*//                                  

|017| using namespace std;                                                                            

|018| int A[100001],n,Max_L[100001],Max_R[100001];                                                    

|019| int main(){                                                                                     

|020| .       while(scanf("%d",&n)!=EOF&&n){                                                          

|021| .       .       for(int i=1;i<=n;++i)                                                           

|022| .       .         scanf("%d",A+i);                                                              

|023| .       .       int _max=-9999999,temp=-9999999;                                                

|024| .       .       for(int i=1;i<n;++i){                                                           

|025| .       .       .       if(temp>=0)                                                             

|026| .       .       .         temp+=A[i];                                                           

|027| .       .       .       else                                                                    

|028| .       .       .       .       temp=A[i];                                                      

|029| .       .       .       _max=_max>temp?_max:temp;                                               

|030| .       .       .       Max_L[i]=_max;                                                          

|031| .       .       }                                                                               

|032| .       .       _max=temp=-999999;                                                              

|033| .       .       for(int i=n;i>1;--i){                                                           

|034| .       .       .       if(temp>0)                                                              

|035| .       .       .         temp+=A[i];                                                           

|036| .       .       .       else                                                                    

|037| .       .       .         temp=A[i];                                                            

|038| .       .       .       _max=_max>temp?_max:temp;                                               

|039| .       .       .       Max_R[i]=_max;                                                          

|040| .       .       }                                                                               

|041| .       .       int res=-999999;                                                                

|042| .       .       for(int i=1;i<n;++i){                                                           

|043| .       .       .       res=(res>Max_L[i]+Max_R[i+1]?res:Max_L[i]+Max_R[i+1]);                  

|044| .       .       }                                                                               

|045| .       .       printf("%d\n",res);                                                             

|046| .       }                                                                                       

|047| .       return 0;                                                                               

|048| }                                                                                               

end+--------------------------------------------------------------------------------------------------+

你可能感兴趣的:(poj)