1. 程式人生 > >[HDU1003]最長子序列和

[HDU1003]最長子序列和

typedef clu emp logs sum ring mes include else

http://acm.hdu.edu.cn/showproblem.php?pid=1003

解題關鍵:1、最大連續子序列和模板

    2、max、end不能使用,在oj中會顯示編譯錯誤

    3、註意模板中沒有else if

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #define INF 10000000 
 6 using namespace std;
 7 typedef long long ll;
8 int n,a[1000002],st,end1; 9 int max_sub(){ 10 int max1=-INF,temp_sum=0,k=0; 11 for(int i=0;i<n;i++){ 12 temp_sum+=a[i]; 13 if(temp_sum>max1){ max1=temp_sum;st=k;end1=i;} 14 if(temp_sum<0){ temp_sum=0;k=i+1;} 15 } 16 return max1; 17 } 18 int main(){
19 int t; 20 scanf("%d",&t); 21 for(int i=1;i<=t;i++){ 22 scanf("%d",&n); 23 for(int j=0;j<n;j++){ 24 scanf("%d",a+j); 25 } 26 int ans=max_sub(); 27 printf("Case %d:\n%d %d %d\n",i,ans,st+1,end1+1); 28 if(i!=t) printf("
\n"); 29 } 30 return 0; 31 }

[HDU1003]最長子序列和