Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我不知道这样算不算DP,不过比那些代码要正确In Reply To:呵呵,求一个DP的CODE,哪位有啊. Posted by:HYSBZ at 2005-08-22 12:38:31 呵呵,还有很多地方可以优化 #include<stdio.h> #include<math.h> int used[45000],loc[45000][10]; main() { int a[100],n,i,j,k,l,tloc; long sum,half,cha,min,minrec,small; scanf("%d",&n); sum=0; for(i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } half=sum/2; min=sum; minrec=sum; used[sum]=1; for(i=0;i<n;i++) { if(!(half-minrec))break; for(j=min;j<=sum;j++) { if(used[j]) for(l=0;l<used[j];l++) { cha=j-a[i]; if(cha<min&&cha>=0)min=cha; if(cha<=sum&&cha>=0) tloc=loc[j][l]+1; if((tloc==n/2||tloc==n/2+n%2)&&abs(half-cha)<abs(half-minrec))minrec=cha; if(used[cha]) { for(k=0;k<used[cha];k++) if(tloc==loc[cha][k])break; if(k>=used[cha]) { loc[cha][used[cha]]=tloc; used[cha]++; } } else { loc[cha][used[cha]]=tloc; used[cha]++; } } } } if(minrec<=half)small=minrec; else small=sum-minrec; printf("%ld %ld",small,sum-small); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator