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 } 

优质内容筛选与推荐>>
1、WorldWind源码剖析系列:地形访问器类TerrainAccessor
2、poj 1979(dfs)
3、iOS开发-OpenGLES 的 GLSL
4、关于window.getSelection
5、Python数据特征分析


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn