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 buptintxy at 2013-05-19 20:05:19 on Problem 1742
#include <stdio.h>
#include <string.h>

int main(int argc, char * argv[])
{
    int weight[100];
    int count [100];
    int capacity, size;

    int available[100001];
    int direction[100001];
    int distance [100001];
    int answer;
    
    int i, j;

    for ( ; ;  )
    {
        scanf("%d%d", &size, &capacity);
        if ( ( capacity == 0 ) && ( size == 0 ) )
        {
            break;
        }
        for ( i = 0; i < size; ++i )
        {
            scanf("%d", &weight[i]);
        }
        for ( i = 0; i < size; ++i )
        {
            scanf("%d", &count[i]);
        }

        answer = 0;
        memset(available, 0, sizeof(available));
        memset(direction, 0, sizeof(direction));
        memset(distance,  0, sizeof(distance ));
        available[0] = 1;
        for ( i = 0; i < size; ++i )
        {
            for ( j = weight[i]; j <= capacity; ++j )
            {
                if ( available[j] == 1 )
                {
                    continue;
                }
                if ( available[ j - weight[i] ] == 0 )
                {
                    continue;
                }
                if ( ( direction[ j - weight[i] ] == ( i + 1 ) ) && ( distance[ j - weight[i] ] == count[i] ) )
                {
                    continue;
                }
                available[j] = 1;
                direction[j] = ( i + 1 );
                distance [j] = ( direction[ j - weight[i] ] == ( i + 1 ) ? distance[ j - weight[i] ] + 1 : 1 );
                answer++;
            }
        }

        printf("%d\n", answer);
    }
    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