Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

弱弱的问一下哪里错了

Posted by foreverago at 2011-07-29 17:17:43 on Problem 1717
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator