Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created February 26, 2018 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maehrm/62df497716811c83b17b5fc93443b343 to your computer and use it in GitHub Desktop.
Save maehrm/62df497716811c83b17b5fc93443b343 to your computer and use it in GitHub Desktop.
Combinatorial - Coin Changing Problemの解答(ただし2次元配列)
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n, m;
int c[21];
int dp[21][50001];
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> c[i];
for (int i = 0; i <= n; i++) {
dp[1][i] = i;
}
for (int i = 2; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (j >= c[i]) {
dp[i][j] = min(dp[i][j-c[i]]+1, dp[i-1][j]);
}
else {
dp[i][j] = dp[i-1][j];
}
}
}
cout << dp[m][n] << endl;
return 0;
}
@maehrm
Copy link
Author

maehrm commented Feb 26, 2018

コイン問題 | 動的計画法 | Aizu Online Judge http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_A&lang=jp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment