Week11 Assignment-E-Optional Question 11-1 Dongdong and ATM

topic

A bank plans to install a machine for cash withdrawal.

The machine can send appropriate bills for the required amount of cash.

The machine uses exactly N banknotes of different denominations, such as D_k, k = 1,2,...,N, and for each denomination D_k, the machine has n_k banknotes.

E.g,

N = 3,

n_1 = 10,D_1 = 100,

n_2 = 4,D_2 = 50,

n_3 = 5,D_3 = 10

It means that the machine has 10 banknotes with a denomination of 100, 4 banknotes with a denomination of 50, and 5 banknotes with a denomination of 10.

Dongdong is writing an ATM program that can request the machine to deliver cash based on the specific amount.

Note that the maximum cash calculated by this program is less than or equal to the cash that can be effectively delivered based on the equipment's available bill supply.

Input

Program input comes from standard input. Each data set in the input represents a specific transaction, and its format is: Cash N n1 D1 n2 D2… nN DN where 0 <= Cash <= 100000 is the amount of cash requested, 0 <= N <= 10 is the denomination of banknotes Quantity, 0 <= nk <= 1000 is the number of usable banknotes of Dk denomination, 1 <= Dk <= 1000, k = 1, N. Spaces can appear between the numbers in the input. The input data is correct.

Output

For each set of data, the program will print the result to the standard output on a separate line in the next line.

Sample Input

735 3 4 125 6 5 3 350
633 4 500 30 6 100 1 5 0 1
735 0
0 3 10 100 10 50 10 10

Sample Output

735
630
0
0
0

Hint

The first data set specifies a transaction in which the requested cash amount is 735. The machine contains 3 denominations of banknotes: 4 banknotes 125, 6 banknotes 5 and 3 banknotes 350. The machine can deliver the exact amount of cash required.
In the second case, the machine's bill supply cannot meet the exact amount of cash required. The maximum cash that can be delivered is 630. Please note that there are multiple possibilities for combining banknotes in the machine to match the cash delivered.
In the third case, the machine is empty and no cash is delivered. In the fourth case, the requested cash amount is 0, so the machine does not deliver cash.

Ideas

1. This question can be regarded as a problem of multiple backpacks. The total number of coins to be composed is regarded as the capacity of the backpack. Each coin of different denomination is an item, and its volume and value are both the face value of the coin.
2. An important algorithm that needs to be used is binary splitting. Binary splitting can split one denomination into other denominations, and these split denominations can combine all the combinations that can be combined by the original denomination, but after splitting, it can be converted into a 01 knapsack problem.
3. After the split is completed, you can directly carry out the 01 backpack, and the corresponding capacity, value and other variables in the 01 backpack should be appropriately changed.

Code

#include<iostream>
int n[20];
int d[20];
int vv[10020];
int f[100020];
using namespace std;
int main()
{
    
    
	int cash,N; 
	while(scanf("%d",&cash)!=EOF)
	{
    
    
		scanf("%d",&N);
		for(int i=1;i<=N;i++)
		{
    
    
			scanf("%d",&n[i]);
			scanf("%d",&d[i]);
		}		
	int cnt=0;
	for(int i=1;i<=N;i++)
	{
    
    
		int t=n[i];
		for(int k=1;k<=t;k<<=1)
		{
    
    
			cnt++;
			vv[cnt]=k*d[i];
			t-=k;
		}
		if(t>0)
		{
    
    
			cnt++;
			vv[cnt]=t*d[i];
		}
	 } 	
	 for(int i=0;i<=cash;i++)
	 	f[i]=0;
	for(int i=1;i<=cnt;i++)
	{
    
    
		for(int j=cash;j>=vv[i];j--)
		{
    
    
			f[j]=max(f[j],f[j-vv[i]]+vv[i]);
		}
		}
		cout<<f[cash]<<endl;	
	}

	return 0;
}

Guess you like

Origin blog.csdn.net/alicemh/article/details/105893903