#include <iostream> #include <algorithm> #include <stdio.h> #include <math.h> #include <map> #include <set> #include <vector> #include <string> #include <cstring> #include <sstream> #include <queue> #include <stack> using namespace std; #define input freopen("input.txt","r",stdin) #define output freopen("output.txt","w",stdout) #define For1(i,a,b) for (i=a;i<b;i++) #define For2(i,a,b) for (i=a;i<=b;i++) #define Dec(i,a,b) for (i=a;i>b;i--) #define Dec2(i,a,b) for (i=a;i>=b;i--) #define Sca_d(x) scanf("%d",&x) #define Sca_s(x) scanf("%s",x) #define Sca_c(x) scanf("%c",&x) #define Sca_f(x) scanf("%f",&x) #define Sca_lf(x) scanf("%lf",&x) #define Fill(x,a) memset(x,a,sizeof(x)) #define MAXN 100005 int n; int num[MAXN]; int before[MAXN]; int main() { //input; int i,start,end,ans,pos,mmax; while(cin>>n,n) { Fill(num,0); Fill(before,0); start=end=0; ans=mmax=0; int flag=1; For2(i,0,n-1) { Sca_d(num[i]); if (num[i]>=0) flag=0;//为了判断全是负数的情况 before[i]=num[i]+before[i-1];//只记录最大值的终点位置,起点位置用前缀和搜 //printf("%d ",before[i]); } For2(i,0,n-1) { ans+=num[i]; if (ans>mmax)//更新最大值 mmax=ans,end=num[i],pos=i; else if (ans<0)//如果当前加的和小于0,那么肯定不会是最优解重新开始 ans=0; } if (flag)//Case1:所有的数都是负数 printf("0 %d %d\n",num[0],num[n-1]); else if (mmax==end)//Case2:一个数就是最大值 //为了防止第一个数就是最大数 printf("%d %d %d\n",mmax,end,end); else { For2(i,0,pos) if (before[pos]-before[i]==mmax)//前缀和求起始值 { start=num[i+1]; break; } printf("%d %d %d\n",mmax,start,end); } } return 0; }
时间: 2024-10-14 09:27:40