tags: dp
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
There are n coins with different denominations, and the denomination of each coin is stored in the array T[1:n]. Now I want to use these coins to find money. The number of coins of various denominations that can be used is stored in the array Coins[1:n].
For any amount of money 0≤m≤20001, design a method to find money m with the least number of coins.
For a given 1≤n≤10, the coin denomination array T and the coin number array Coins of various denominations that can be used, and the amount of money m, 0≤m≤20001, calculate Find the minimum number of coins for money m.
Input
In the first line of the input data, there is only 1 integer giving the value of n, and 2 numbers in each line starting from the second line, which are T[j] and Coins[j]. The last line is m.
Output
The output data has only one integer, which represents the minimum number of coins calculated. When the problem has no solution, -1 is output.
Sample Input
3
1 3
2 3
5 3
18
Sample Output
5
Hint
Source
//There are restrictions on the types of coins and the number of coins
//Multiple backpacks
#include <iostream>
//#include <cstdio>
using namespace std;
const int maxvalue = 20001;
const int coinnum = 15;
int mymin(int a, int b)
{
return a < b ? a : b;
}
int main()
{
int n;
cin >> n;
int coin[coinnum];
int coincount[coinnum];
for(int i = 0; i < n; i++)
{
cin >> coin[i];
cin >> coincount[i];
}
int m;
cin >> m;
//The amount of money is dp[i] is the number of coins
int *dp = new int[maxvalue]();
//Key error
//for(int i = 0; i <= m; i++) is wrong
for(int i = 1; i <= m;i++)
dp[i] = maxvalue;
int i, j, k;
for(i = 0; i < n; i++)//The number of coin denominations
{
for(j = 1; j <= coincount[i]; j++)//The number of denominations of coins
{
for(k = m; k >= coin[i]; k--)
{
//The dynamic migration equation is
dp[k] = mymin(dp[k - coin[i]] + 1, dp[k]);
}
}
}
if(dp[m] == maxvalue)
cout << -1 << endl;
else cout << dp[m] << endl;
return 0;
}
source: 2009 Multi-University Training Contest 3 - Host by WHU Question: There are n different kinds of coins, and the number of each coin is given. How many kinds of prices can be combined from these...
Multiple backpacks can be converted to a 01 backpack, first decompose the number C binary decomposition into a set of multiple numbers, which can be combined to arbitrarily less than or equal to C, an...
Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wante...
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 878 Accepted Submission(s): 357 Problem Description Whuacmers use coins.They have coins of v...
The meaning: Ask the given coin how many money can be constructed in 1 ~ V Idea: Bare...
Set with multiple backpack templates, if this code is found in Poj 1742, please see http://blog.csdn.net/hao_zong_yin/Article/details/72585105...
Question J: Coins IV Time limit: 1 Sec memory limit: 128 MB Submitted: 5 Solution: 4 [submit] [status] [Discussion board] [Propositional person:admin] Description of the topic People in Silverland use...
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watc...
POJ–1742 Coins N given type of coin, wherein the i th coin denominations of Ai, a total Ci. To choose the number of coins, the face value of the sum, if the result is S, called "par value S...