In the country of ALPC , Xiaoqian is a very famous mathematician. She is immersed in calculate, and she want to use the minimum number of coins in every shopping. (The numbers of the shopping include the coins she gave the store and the store backed to her.)
And now , Xiaoqian wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1, V2, ..., VN (1 ≤ Vi ≤ 120). Xiaoqian is carrying C1 coins of value V1, C2 coins of value V2, ...., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner .But Xiaoqian is a low-pitched girl , she wouldn’t like giving out more than 20000 once.
And now , Xiaoqian wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1, V2, ..., VN (1 ≤ Vi ≤ 120). Xiaoqian is carrying C1 coins of value V1, C2 coins of value V2, ...., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner .But Xiaoqian is a low-pitched girl , she wouldn’t like giving out more than 20000 once.
Line 1: Two space-separated integers: N and T.
Line 2: N space-separated integers, respectively V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ..., CN
The end of the input is a double 0.
3 70 5 25 50 5 2 1 0 0
Case 1: 3
题意:有N中规格的钱币,需要购买价值T的东西,如果没有价值恰好的钱币支付,店铺会找给你相应规格的钱币,给出每种钱币的价值和数量,求购买东西所需要经过几次钱币的转移。
分析:当做一个背包问题来做,不要被题目的T迷惑,背包上限是题中所给的20000。对给钱做混合背包处理,对找钱做多重背包处理,分别用两个数组dp1[i], dp2[i]记录钱币的转移次数。最后用dp1和dp2之间的联系找出答案。
代码如下:
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mod 835672545
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MX = 2e4 + 5;
const int N = 105;
int n, t;
int v[N], c[N];
int dp1[MX], dp2[MX];
void zeroOnePack(int v, int k){
for(int i = MX-5; i >= v; i--){
dp1[i] = min(dp1[i], dp1[i-v] + k);
}
}
void completePack(int v){
for(int i = v; i <= MX-5; i++){
dp1[i] = min(dp1[i], dp1[i-v] + 1);
}
}
void mutiPack(int v, int w){
if(v * w >= MX-5){
completePack(v);
return ;
}
int k = 1;
while(k < w){
zeroOnePack(v*k, k);
w -= k;
k <<= 1;
}
zeroOnePack(v*w, w);
}
int main(){
int cas = 1;
while(~scanf("%d%d", &n, &t), n&&t){
memset(dp1, INF, sizeof(dp1));
memset(dp2, INF, sizeof(dp2));
dp1[0] = dp2[0] = 0;
for(int i = 1; i <= n; i++){
scanf("%d", &v[i]);
}
for(int i = 1; i <= n; i++){
scanf("%d", &c[i]);
}
for(int i = 1; i <= n; i++){ //给钱
mutiPack(v[i], c[i]);
}
for(int i = 1; i <= n; i++){ //找钱
for(int j = v[i]; j <= MX-5; j++){
dp2[j] = min(dp2[j], dp2[j-v[i]] + 1);
}
}
int ans = INF;
for(int i = t; i <= MX-5; i++){
ans = min(ans, dp1[i] + dp2[i-t]);
}
printf("Case %d: %d\n", cas++, ans == INF ? -1 : ans);
}
return 0;
}