首页 > 其他 > 详细

[HDU1003]最长子序列和

时间:2017-05-10 21:13:08      阅读:296      评论:0      收藏:0      [点我收藏+]

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]最长子序列和

原文:http://www.cnblogs.com/elpsycongroo/p/6837924.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!