Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 37834 Accepted Submission(s): 17032
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int maxn = 1e5+5; int dp[maxn]; int sum[maxn]; int a[maxn]; int main(){ int n; while(scanf("%d",&n) &&n){ for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } int left=1,right=1; int t=a[1]; memset(dp,0,sizeof(dp)); dp[1]=0; for(int i=2;i<=n;i++){ dp[i]=0; } for(int i=2;i<=n;i++){ dp[i]=max(a[i],dp[i-1]+a[i]); if(dp[i]>t){ right=i; t=dp[i]; } } int sum=0; for(int i=right;i>0;i--){ sum+=a[i]; if(sum==t){ left=i; break; } } if(t<0){ printf("0 %d %d\n",a[1],a[n]); continue; } printf("%d %d %d\n",t,a[left],a[right]); } }
dp[i] = max(a[i],dp[i-1]+a[i])
原文:https://www.cnblogs.com/buerdepepeqi/p/9153077.html