您的位置:首页 > 其它

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

2013-03-23 13:08 281 查看
View Code

/*
最大连续上升子序列和
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
//#include<map>
#include<math.h>
using namespace std;
typedef long long ll;
//typedef __int64 int64;
const int maxn = 100005;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-8;
int a[ maxn ];
int pos_s,pos_t,max_sum;

void LIS( int a[],int n ){
int tmp_pos,tmp_sum ;
pos_s = pos_t = tmp_pos = 1;
max_sum = tmp_sum = a[ 1 ];
for( int i=2;i<=n;i++ ){
if( tmp_sum+a[ i ]<a[ i ] ){
tmp_sum = a[ i ];
tmp_pos = i;
}
else{
tmp_sum += a[ i ];
}//当前 tmp_sum 的值就表示前面所有的最大的值的那个,且一定包含a[i-1]
if( tmp_sum>max_sum ){
max_sum = tmp_sum;
pos_s = tmp_pos;
pos_t = i;
}//从1到n扫描 暗示 着 pos_s最小,如果存在多个ans的话
}
}

int main(){
int ca;
scanf("%d",&ca);
for( int t=1;t<=ca;t++ ){
if( t!=1 ) printf("\n");
int n;
scanf("%d",&n);
for( int i=1;i<=n;i++ )
scanf("%d",&a[ i ]);
LIS( a,n );
printf("Case %d:\n%d %d %d\n",t,max_sum,pos_s,pos_t);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: