问题 G: Optimal Coin Change
时间限制: 1 Sec 内存限制: 128 MB提交: 79 解决: 60
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
In a 10-dollar shop, everything is worthy 10 dollars or less. In order to serve customers more effectively at the cashier, change needs to be provided in the minimum number of coins.
In this problem, you are going to provide a given value of the change in different coins. Write a program to calculate the number of coins needed for each type of coin.
The input includes a value v, a size of the coinage set n, and a face value of each coin, f1, f2, ..., fn. The output is a list of numbers, namely, c1, ..., cn, indicating the number of coins needed for each type of coin. There may be many ways for the change. The value v is an integer satisfying 0 < v ≤ 2000, representing the change required
in cents. The face value of a coin is less than or equal to 10000. The output of your program should take the combination with the least number of coins needed.
For example, the Hong Kong coinage issued by the Hong Kong Monetary Authority consists of 10 cents, 20 cents, 50 cents, 1 dollar, 2 dollars, 5 dollars and 10 dollars would be represented in the input by n = 7, f1 = 10, f2 = 20, f3 = 50, f4 = 100, f5 = 200, f6 = 500, f7 = 1000.
In this problem, you are going to provide a given value of the change in different coins. Write a program to calculate the number of coins needed for each type of coin.
The input includes a value v, a size of the coinage set n, and a face value of each coin, f1, f2, ..., fn. The output is a list of numbers, namely, c1, ..., cn, indicating the number of coins needed for each type of coin. There may be many ways for the change. The value v is an integer satisfying 0 < v ≤ 2000, representing the change required
in cents. The face value of a coin is less than or equal to 10000. The output of your program should take the combination with the least number of coins needed.
For example, the Hong Kong coinage issued by the Hong Kong Monetary Authority consists of 10 cents, 20 cents, 50 cents, 1 dollar, 2 dollars, 5 dollars and 10 dollars would be represented in the input by n = 7, f1 = 10, f2 = 20, f3 = 50, f4 = 100, f5 = 200, f6 = 500, f7 = 1000.
输入
The test data may contain many test cases, please process it to the end of the file.
Each test case contains integers v, n, f1, ..., fn in a line. It is guaranteed that n ≤ 10 and 0 < f1 < f2 < ...< fn.
Each test case contains integers v, n, f1, ..., fn in a line. It is guaranteed that n ≤ 10 and 0 < f1 < f2 < ...< fn.
输出
The output be n numbers in a line, separated by space. If there is no possible change, your output should be a single −1. If there are more than one possible solutions, your program should output the one that uses more coins
of a lower face value.
样例输入
2000 7 10 20 50 100 200 500 1000
250 4 10 20 125 150
35 4 10 20 125 150
48 4 1 8 16 20
40 4 1 10 13 37
43 5 1 2 21 40 80
样例输出
0 0 0 0 0 0 2
0 0 2 0
-1
0 1 0 2
3 0 0 1
1 1 0 1
题意:给你一个钱数v,n种硬币面值,询问是否能用这n种硬币凑出总钱数v,若能凑出则输出最小的每种钱币的使用数量(若有多组答案,输出尽可能使用面额小的钱币)。
题解:对于钱数v,可用dp数组记录在容量为j的情况下,使用的最小钱币数量(完全背包背一下),然后用vis数组记录凑出该金额的上一个金额为多少,最终再用数组记录每种硬币的使用情况输出即可。
#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
int main()
{
int v,a[20],ans[2005],vis[2005],dp[2005];
while(~scanf("%d",&v))
{
memset(dp,INF,sizeof(dp));
memset(vis,0,sizeof(vis));
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
dp[0]=0;
vis[0]=-1;
for(int i=1;i<=n;i++)
for(int j=a[i];j<=v;j++)
if(dp[j-a[i]]+1<=dp[j] && dp[j-a[i]]!=INF) //注意是小于等于,若是小于则不能保证是尽可能多的使用小面额硬币
{
dp[j]=dp[j-a[i]]+1;
vis[j]=j-a[i];
}
if(dp[v]==INF)
printf("-1\n");
else
{
memset(ans,0,sizeof(ans));
int t=v;
while(1)
{
if(vis[t]==-1) break;
ans[t-vis[t]]++;
t=vis[t];
}
printf("%d",ans[a[1]]);
for(int i=2;i<=n;i++)
printf(" %d",ans[a[i]]);
putchar('\n');
}
}
return 0;
}