6 -2 11 -4 13 -5 -2 10 -10 1 2 3 4 -5 -23 3 7 -21 6 5 -8 3 2 5 0 1 10 3 -1 -5 -2 3 -1 0 -2 0
20 11 13 10 1 4 10 3 5 10 10 10 0 -1 -2 0 0 0Huge input, scanf is recommended.HintHint
题意及思路:统计区间最大和,比较烦的是还要写出区间和的左右边界的值,右值好说,左值还要小心处理一下,而且在相同答案的情况下还要输出最左边的一组数据。
#include <iostream> #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string> #include <string.h> #include <algorithm> #include <vector> #include <queue> #include <iomanip> #include <time.h> #include <set> #include <map> #include <stack> using namespace std; typedef long long LL; const int INF=0x7fffffff; const int MAX_N=10000; int K; int A[10009]; int main(){ while(scanf("%d",&K)&&K!=0){ int flag=0; for(int i=0;i<K;i++){ scanf("%d",&A[i]); if(A[i]>=0){ flag=1; } } if(flag==0){ cout<<0<<" "<<A[0]<<" "<<A[K-1]<<endl; } else{ int ans=-1,ansl=A[0],ansr,curans=0,curl; for(int i=0;i<K;i++){ if(A[i]>0){ curans+=A[i]; if(curans-A[i]<=0){ curl=A[i]; } if(curans>ans){ ans=curans; ansr=A[i]; ansl=curl; } } else{ curans+=A[i]; if(curans<0){ curans=0; } if(A[i]==0&&curans>ans){ ansl=0; ansr=0; ans=0; } } } cout<<ans<<" "<<ansl<<" "<<ansr<<endl; } } return 0; }