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 |
弱弱的问一下哪里错了#include <stdio.h> #define N 10005 #define INF 0x7ffffff int dp[N]; int a[1005],b[1005]; int MIN(int a,int b) { return a<b?a:b; } int ABS(int a) { return a<0?-a:a; } int main() { freopen("in.txt","r",stdin); int n,sum=0,aa=0,bb=0; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d %d",&a[i],&b[i]); sum=sum+a[i]+b[i]; aa+=a[i]; bb+=b[i]; } for(int i=0; i<=sum; i++) dp[i]=INF; dp[aa]=0; int m; for(int i=1; i<=n; i++) { m=b[i]-a[i]; if(m<0) { for(int j=-m; j<=sum; j++) if(dp[j]!=INF) dp[j+m]=MIN(dp[j]+1,dp[j+m]); } if(m>0) { for(int j=sum-m; j>=0; j--) { if(dp[j]!=INF) { dp[j+m]=MIN(dp[j]+1,dp[j+m]); } } } } int min=INF,ans; for(int i=0; i<=sum; i++) if(dp[i]!=INF) { if(min>ABS(sum-i-i)) { min=ABS(sum-i-i); ans=dp[i]; } } printf("%d\n",ans); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator