/*
http://acm.hdu.edu.cn/showproblem.php?pid=1003
最大字段和,并输出区间,再弄一个数组保存每一个sum的区间,
input
5 6 -1 5 4 -7
sum 6 5 10 14 7
[0,0][0,1][0,2][0,3][0,4]
遍历的时候取到最大和的时候输出区间即可
*/
#define N // my
//#define test
#ifdef test
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <time.h>
using namespace std;
#define CLR(c,v) (memset(c,v,sizeof(c)))
double rands(int _Begin,int _End){
double x = rand() % ( _End - _Begin ) + _Begin ;
return x;
}
int main()
{
freopen("Input.txt","w",stdout);
srand( (unsigned)time( NULL ) );
rands(10,20); //[10,20)的随机数
int Ncase = 100;
cout << Ncase << endl;
for (int i = 0 ; i < Ncase ; i++){
int n = rands(1 , 40);
cout << n ;
for (int j = 0 ; j < n ; j++){
printf(" %d ",(int)rands(-10000,20000));
}
cout << endl;
}
return 0;
}
#endif
#ifdef N
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
#define CLR(c,v) (memset(c,v,sizeof(c)))
const int inf = -(1<<20);
const int M = 1e6 + 10;
int seq[M];
int sum[M]; // 100 000 * 4 byte
struct dec{
int x,y;
}d[M]; // 每一个和的位置
int MaxValue(int s[],int len){
int max = inf, cnt = 0 , max_i = 0 ;
sum[cnt] = s[0] ;
d[0].x = 0;
d[0].y = 0;
for (int i = 1 ; i < len ; i++){
if (sum[cnt] >= 0){
d[cnt+1].x = d[cnt].x;
d[cnt+1].y = d[cnt].y + 1;
sum[cnt+1] = sum[cnt++] + s[i];
}else{
d[cnt+1].x = i;
d[cnt+1].y = i;
sum[++cnt] = s[i];
}
}
for (int i = 0 ; i < len ; i++){
if (max < sum[i]){
max = sum[i];
max_i = i;
}
}
printf("%d %d %d\n" , max , d[max_i].x+1 , d[max_i].y+1 );
return max;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("Input.txt","r",stdin);
//freopen("out.txt","w",stdout);
int Ncase , len, n=1;
cin >> Ncase;
while(Ncase -- ){
cin >> len;
CLR(sum,0);
for (int i = 0 ; i < len ; i++){
scanf("%d",&seq[i]);
}
if (n != 1 )cout << endl;
printf("Case %d:\n",n++);
MaxValue(seq,len) ;
}
return 0;
}
#endif
HDU 1003 Max Sum - 简单动态规划
最新推荐文章于 2017-08-15 09:43:30 发布