关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回

hdu1003

发布时间:2022-07-27 13:56:06

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 95590    Accepted Submission(s): 22086

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.   Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000). Output For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.   Sample Input 2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5   Sample Output Case 1: 14 1 4 Case 2: 7 1 6              
  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4. int main()
  5. {  
  6. int i,j=0,t;
  7.  scanf("%d",&t);
  8.  while(t--)
  9.  {   
  10. int  n,sum=0,max=-9999999,start,end=0,k=0,;
  11.   scanf("%d",&n);   
  12. for(i=0;i<n;i++)  
  13.  {   
  14.  int a; 
  15.  scanf("%d",&a);  
  16.   sum+=a;  
  17.   if(sum>max)  
  18.   {     
  19. max=sum;    
  20.  start=k+1;  
  21.    end=i+1;   
  22.  }    
  23. if(sum<0)   
  24.  {    
  25.  sum=0;    
  26.  k=i+1;   
  27.  }  
  28.  }        
  29.   cout<<"Case "<<++j<<":"<<endl;    
  30.            cout<<max<<" "<<start<<" "<<end<<endl;   
  31.    if(t>0)     
  32.   cout<<endl;
  33.  }  
  34. return 0;
  35. }
 

/template/Home/DawnNew/PC/Static

立即注册风纳云账号,免费体验多款产品

立即注册