标签:
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
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
Sample Output
20 11 13 10 1 4 10 3 5 10 10 10 0 -1 -2 0 0 0 Huge input, scanf is recommended.
Hint
想想还是挺好的的题,dp吧,就是最开始的比较复杂,后面看了别人的代码 选择用数组就存,自己还是太弱了,
还需要好好努力呀,知道如何找第一个位置 就很简单了,以下是我的代码
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> typedef long long ll; #define N 10005 using namespace std; int a[N]; int dp[N]; int main() { int t; int i,j; int flag; int maxsum=0; int b[N]; while(scanf("%d",&t)!=EOF) { if(t==0) break; flag=0; maxsum=0; memset(a,0,sizeof(a)); for(i=1;i<=t;i++) { scanf("%d",&a[i]); if(a[i]>=0) flag=1; } memset(dp,0,sizeof(dp)); dp[0]=0; int first=1; for(i=1;i<=t;i++) { if(dp[i-1]+a[i]>=a[i]) { dp[i]=dp[i-1]+a[i]; } else { dp[i]=a[i]; first=i; } maxsum=max(maxsum,dp[i]); b[i]=first; } int next; if(flag==0) printf("0 %d %d\n",a[1],a[t]); else { for(i=1;i<=t;i++) { if(maxsum==dp[i]) { next=i; break; } } //cout<<next<<" "<<a[0]<<endl; printf("%d %d %d\n",maxsum,a[b[next]],a[next]); } } }
标签:
原文地址:http://www.cnblogs.com/Aa1039510121/p/5877161.html