HDU-3348-coins
链接:https://vjudge.net/problem/HDU-3348
题意:
给1,5,10,50,100面值的钞票个a张,求得到p的最小张数和最大张数,
不能则输出-1.
思路:
贪心,正常求最小张数,然后讲所有钞票能表示的总和,减去原本求的价值。
求这个的最小张数,剩下的则是原本价值的最大张数。
代码:
#include <iostream> #include <memory.h> #include <string> #include <istream> #include <sstream> #include <vector> #include <stack> #include <algorithm> #include <map> #include <queue> #include <math.h> #include <cstdio> using namespace std; typedef long long LL; int a[10] = {0, 1, 5, 10, 50, 100}; int v[10]; int Get_M(int value) { int res = 0; for (int i = 5;i >= 1;i--) { if (value >= a[i]) { int tmp = value / a[i]; value -= a[i] * min(tmp, v[i]); res += min(tmp, v[i]); } } if (value != 0) return -1; return res; } int main() { int t; cin >> t; while (t--) { int p; int sum = 0, num = 0; cin >> p; for (int i = 1;i <= 5;i++) { cin >> v[i]; num += v[i]; sum += v[i] * a[i]; } int res_min = 0, res_max = 0; res_min = Get_M(p); res_max = Get_M(sum - p); if (res_min == -1 || res_max == -1) cout << -1 << ' ' << -1 << endl; else cout << res_min << ' ' << num - res_max << endl; } return 0; }