zoukankan      html  css  js  c++  java
  • 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 } 
    keep moving...
  • 相关阅读:
    HDU 3068 Manacher
    HDU 6188最小费用流
    Codeforces Round #442 (Div. 2) Danil and a Part-time Job
    并查集
    HDU 5988最小网络流(浮点数)
    HOJ
    HOJ
    POJ
    POJ
    关于async
  • 原文地址:https://www.cnblogs.com/xxx0624/p/2977056.html
Copyright © 2011-2022 走看看