[HDU1003]最长子序列和

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 }

 

 1 #include<bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 using namespace std;
 4 typedef long long ll;
 5 int a[50002];
 6 int main(){
 7     int n;
 8     cin>>n;
 9     ll sum=0,max=0;
10     for(int i=0;i<n;i++){
11         cin>>a[i];
12         //a[i]=-a[i];
13         sum+=a[i];
14         if(sum>max) max=sum;
15         if(sum<0) sum=0;
16     }
17     printf("%lld\n",max);
18 }

 

posted @ 2017-05-10 20:22  Elpsywk  阅读(179)  评论(0编辑  收藏  举报