-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakingChange_UVA166OJ.cpp
74 lines (62 loc) · 1.81 KB
/
makingChange_UVA166OJ.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
const int inf = INT_MAX / 2, N = 1000, ctc = 6;
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
vector<int> coins = {inf, 5, 10, 20, 50, 100, 200};
vector<vector<int>> dp_infc(ctc + 1, vector<int>(N + 1, inf)); dp_infc[0][0] = 0;
for (int i = 1; i <= ctc; ++i)
{
for (int j = N; j >= 0; --j)
{
for (int c = 0; coins[i] * c <= j; ++c)
{
dp_infc[i][j] = min(dp_infc[i - 1][j], dp_infc[i - 1][j - coins[i] * c] + c);
}
}
}
while (true)
{
vector<int> cc(ctc + 1, inf);
for (int i = 1; i <= ctc; ++i)
{
cin >> cc[i];
}
bool canBreak = true;
for (int i = 1; i <= ctc; ++i)
{
if (cc[i] > 0)
{
canBreak = false;
break;
}
}
if (canBreak == true) break;
double pay_d; cin >> pay_d;
int pay = (int)(pay_d * 100.0 + 0.5);
vector<vector<int>> dp(ctc + 1, vector<int>(N + 1, inf)); dp[0][0] = 0;
for (int i = 1; i <= ctc; ++i)
{
for (int j = N; j >= 0; --j)
{
for (int c = 0; coins[i] * c <= j && c <= cc[i]; ++c)
{
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - coins[i] * c] + c);
}
}
}
int ans = inf;
for (int p = pay; p <= N; ++p)
{
if (ans > dp[ctc][p] + dp_infc[ctc][p - pay])
{
ans = dp[ctc][p] + dp_infc[ctc][p - pay];
}
}
cout << setw(3) << ans << '\n';
}
cout.flush();
return 0;
}