HDOJ1003(Max Sum)dp之最大子段和

#include <iostream>
using namespace std;

int
 main()
{
    
int
 s, e, k;
    
int
 result, tmp, cc;
    
int
 T, n, i, j;
    
while(cin>>
T)
        
for(j = 1; j <= T; j++
)
        
{
            
//==============此段位初始化======================


            result 
= -9999;
            tmp 
= 0
;
            s 
= e = k = 1
;

            
//==============此段位初始化======================


            cin
>>n;
            
for(i = 1; i <= n; i++
)
            
{
                cin
>>
cc;
                tmp 
+=
 cc;
                
if(tmp >
 result)
                
{
                    s 
=
 k;
                    e 
=
 i;
                    result 
=
 tmp;
                }

                
if(tmp < 0)
                
{
                    k 
= i + 1
;
                    tmp 
= 0
;
                }

            }

            cout
<<"Case "<<j<<":"<<endl;
            cout
<<result<<" "<<s<<" "<<e<<
endl;
            
if(j !=
 T)
                cout
<<
endl;
        }

    
return 0;
}

posted on 2009-03-23 09:44  Xredman  阅读(302)  评论(0编辑  收藏  举报

导航