poj 3260 The Fewest Coins
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7311 | Accepted: 2255 |
Description
Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus the number of coins he receives in change is minimized. Help him to determine what this minimum number is.
FJ 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). Farmer John 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 (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).
Input
Line 2: N space-separated integers, respectively V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ..., CN
Output
Sample Input
3 70 5 25 50 5 2 1
Sample Output
3
Hint
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<vector> #include<cstring> #include<string> #include<cmath> using namespace std; const int INF = 0x3f3f3f3f; const int N_MAX = 100 + 20; const int V_MAX = 120 + 10; const int T_MAX = 10000 + 10; typedef long long ll; int n, t; int v[N_MAX], c[N_MAX]; int dp_pay[T_MAX + V_MAX*V_MAX], dp_rec[T_MAX + V_MAX*V_MAX]; void pack_pay(int W) { dp_pay[0] = 0; for (int i = 0; i < n; i++) { int num = c[i]; for (int k = 1; num>0; k <<= 1) { int mul = min(k, num); for (int j = W; j >= v[i] * mul; j--) { dp_pay[j] = min(dp_pay[j], dp_pay[j - v[i] * mul] + mul); } num -= mul; } } } void pack_rec(int W) { dp_rec[0] = 0; for (int i = 0; i < n; i++) { for (int j = v[i]; j <= W; j++) { dp_rec[j] = min(dp_rec[j], dp_rec[j - v[i]] + 1); } } } int main() { while (scanf("%d%d", &n, &t) != EOF) { memset(dp_pay, INF, sizeof(dp_pay)); memset(dp_rec, INF, sizeof(dp_rec)); for (int i = 0; i < n; i++) scanf("%d", v + i); for (int i = 0; i < n; i++) scanf("%d", c + i); int max_v = *max_element(v, v + n); pack_pay(t + max_v*max_v); pack_rec(max_v*max_v); int sum = INF; for (int i = 0; i <= max_v*max_v; i++) { sum = min(sum, dp_pay[t + i] + dp_rec[i]); } if (sum == INF)sum = -1; printf("%d\n", sum); } return 0; }