求最大子段和,需要记录开始喝结束的位置.一维dp,注意s和e的初始化。
#include "iostream"
#include "stdio.h"
#include "math.h"
#include "vector"
#include "queue"
#include "memory.h"
#include "algorithm"
#include "string"
using namespace std;
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
int li[10010];
int main(){
int n;
while(scanf("%d",&n)!=EOF&&n){
for(int i=0;i<n;i++)scanf("%d",&li[i]);;
int sum=-1,tmp=0,tmps=0,s=0,e=n-1;
for(int i=0;i<n;i++){
tmp+=li[i];
if(tmp>sum)
{
s=tmps;
e=i;
sum=tmp;
}
else if(tmp<0)
{
tmps=i+1;
tmp=0;
}
}
if(sum<0)sum=0;
printf("%d %d %d\n",sum,li[s],li[e]);
}
}