HDU1003+最大连续子序列和+起始终止位置

View Code
 1 /*
 2 最大连续上升子序列和
 3 */
 4 #include<stdio.h>
 5 #include<string.h>
 6 #include<stdlib.h>
 7 #include<algorithm>
 8 #include<iostream>
 9 #include<queue>
10 //#include<map>
11 #include<math.h>
12 using namespace std;
13 typedef long long ll;
14 //typedef __int64 int64;
15 const int maxn = 100005;
16 const int inf = 0x7fffffff;
17 const double pi=acos(-1.0);
18 const double eps = 1e-8;
19 int a[ maxn ];
20 int pos_s,pos_t,max_sum;
21 
22 void LIS( int a[],int n ){
23     int tmp_pos,tmp_sum ;
24     pos_s = pos_t = tmp_pos = 1;
25     max_sum = tmp_sum = a[ 1 ];
26     for( int i=2;i<=n;i++ ){
27         if( tmp_sum+a[ i ]<a[ i ] ){
28             tmp_sum = a[ i ];
29             tmp_pos = i;
30         }
31         else{
32             tmp_sum += a[ i ];
33         }//当前 tmp_sum 的值就表示前面所有的最大的值的那个,且一定包含a[i-1]
34         if( tmp_sum>max_sum ){
35             max_sum = tmp_sum;
36             pos_s = tmp_pos;
37             pos_t = i;
38         }//从1到n扫描 暗示 着 pos_s最小,如果存在多个ans的话
39     }
40 }
41 
42 int main(){
43     int ca;
44     scanf("%d",&ca);
45     for( int t=1;t<=ca;t++ ){
46         if( t!=1 ) printf("\n");
47         int n;
48         scanf("%d",&n);
49         for( int i=1;i<=n;i++ )
50             scanf("%d",&a[ i ]);
51         LIS( a,n );
52         printf("Case %d:\n%d %d %d\n",t,max_sum,pos_s,pos_t);
53     }
54     return 0;
55 } 

 

转载于:https://www.cnblogs.com/xxx0624/archive/2013/03/23/2977056.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值