-
Notifications
You must be signed in to change notification settings - Fork 4
/
minimum-coin.cpp
106 lines (93 loc) · 1.46 KB
/
minimum-coin.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
const int N = 2e5 + 11;
int n, target;
vector<int> li;
int cache;
int dp[N];
int seq[N];
int possible_min;
int get_min_coin(int t)
{
if (dp[t])
return dp[t];
if (t < 0)
return 10000;
else if (t == 0)
return 0;
else
{
int best = INT32_MAX;
for (int a : li)
{
int calculate = get_min_coin(t - a) + 1;
best = min(best, calculate);
dp[t] = best;
}
return best;
}
}
int get_min_coin_iterative(int t)
{
dp[0] = 0;
int best = INT32_MAX;
for (int i = 1; i <= t; i++)
{
best = 1000;
for (auto x : li)
{
if (i - x >= 0 && best)
{
possible_min = dp[i - x] + 1;
best = min(best, possible_min);
if (best == possible_min)
{
seq[i] = x;
}
}
}
dp[i] = best;
}
}
void solve()
{
cin >> n >> target;
int holder;
for (int i = 0; i < n; i++)
{
cin >> holder;
li.push_back(holder);
}
// cout << get_min_coin(target) << endl;
get_min_coin_iterative(target);
int t = target;
while (t > 0)
{
cout << seq[t];
t -= seq[t];
}
cout << endl;
cout << dp[target] << endl;
}
int main()
{
freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int no_of_test_cases = 0;
// cin >> no_of_test_cases;
if (!no_of_test_cases)
no_of_test_cases = 1;
while (no_of_test_cases--)
{
solve();
}
return 0;
}