标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20439 Accepted Submission(s): 9038
状态转移方程为dp[i]=max(dp[i-1],a[i])
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<stdlib.h> #include<queue> #include<stack> #include<algorithm> #define LL __int64 using namespace std; const int MAXN=10000+50; const int INF=0x3f3f3f3f; const double EPS=1e-9; int dir4[][2]={{0,1},{1,0},{0,-1},{-1,0}}; int dir8[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}}; int dir_8[][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; int n,L,R,a[MAXN],dp[MAXN],maxn,star,en; int main() { //freopen("in.txt","r",stdin); while(scanf("%d",&n) && n) { memset(a,0,sizeof(a)); memset(dp,0,sizeof(dp)); maxn=-INF; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) { if(i==1) star=a[i],dp[i]=a[i]; if(dp[i-1]+a[i]<a[i]) dp[i]=a[i],star=a[i];//更新记录的左边界 else dp[i]=a[i]+dp[i-1]; if(dp[i]>maxn) maxn=dp[i],L=star,R=a[i];//更新最大值的时候同时也更新下记录的边界,右边界没有必要单独更新 } if(maxn>=0) printf("%d %d %d\n",maxn,L,R); else printf("0 %d %d\n",a[1],a[n]); } }
标签:
原文地址:http://www.cnblogs.com/clliff/p/4245846.html