POJ 1276 Cash Machine

Description

A Bank plans to install amachine for cash withdrawal. The machine is able to deliver appropriate @ billsfor a requested cash amount. The machine uses exactly N distinct billdenominations, say Dk, k=1,N, and for each denomination Dk the machine has a supplyof nk bills. For example,

N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10

means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each,and 5 bills of @10 each.

Call cash the requested amount of cash the machine should deliver and write aprogram that computes the maximum amount of cash less than or equal to cashthat can be effectively delivered according to the available bill supply of themachine.

Notes:
@ is the symbol of the currency delivered by the machine. For instance, @ maystand for dollar, euro, pound etc.

Input

The program input is fromstandard input. Each data set in the input stands for a particular transactionand has the format:

cash N n1 D1 n2 D2 ... nN DN

where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <=10 is the number of bill denominations and 0 <= nk <= 1000 is the numberof available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. Whitespaces can occur freely between the numbers in the input. The input data arecorrect.

Output

For each set of data theprogram prints the result to the standard output on a separate line as shown inthe examples below.

Sample Input

735 3  4125  6 5 3 350

633 4  50030  6 100 1 5  0 1

735 0

0 3  10 100  10 50 10 10

Sample Output

735
630
0
0

题目简介:需要的钱cash,有N种钱币,每种有ni张,面值为Di。问最大小于或等于cash的数是多少。

方法:多重背包。开始我直接化成01背包做的,但是超时了。。。。。。。。这道题可以将每种,化成1,2,4,。。。。。,2^(k-1),ni-2^k+1,k是满足ni-2^k+1>0的最大整数。这样可以优化。

 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct Cash
{
	int n, d;
};
typedef struct Cash C;
C num[11];

int f[100010];

int main()
{
	int N, cash;
	int i, j ,k;
	while(scanf("%d",&cash)!=EOF)
	{
		scanf("%d",&N);

		for(i = 0;i<N;i++)
		{
			scanf("%d%d",&num[i].n,&num[i].d);
		}

		int min = 100000 ,all = 0;
		for(i = 0;i<N;i++)
		{
			if(min>num[i].d)
			{
				min = num[i].d;
			}
			all += num[i].d * num[i].n;
		}

		if(min > cash)
		{
			printf("0\n");
			continue;
		}

		if(all < cash)
		{
			printf("%d\n",all);
			continue;
		}

		memset(f,0,sizeof(f));

		for(i = 0;i<N;i++)
		{
			if(num[i].d * num[i].n==cash)
			{
				f[cash] = cash;
				break;
			}
			else if(num[i].d * num[i].n > cash)
			{
				for (k = num[i].d; k <= cash; k++)
				{
					if (f[k - num[i].d] + num[i].d > f[k])
					{
						f[k] = f[k - num[i].d] + num[i].d;
					}
				}
			}
			else
			{
				int m = num[i].n;
                for(j = 1; j <= num[i].n/2;j*=2)
                {
                    for( k = cash; k >= j * num[i].d; k--)
					{
                        if (f[k - j * num[i].d] + j * num[i].d > f[k]) 
						{
							f[k] = f[k - j * num[i].d] + j * num[i].d;
						}
					}
                    m -= j;
                }
                for (k = cash; k >= m * num[i].d; k--)
				{
                    if (f[k - m * num[i].d] + m * num[i].d > f[k])
					{
						f[k] = f[k - m * num[i].d] + m * num[i].d;
					}
				}
			}
		}
		printf("%d\n",f[cash]);
	}
	return 0;
}


 

你可能感兴趣的:(POJ 1276 Cash Machine)