USACO-Stamps
来源:http://ace.delos.com/usacoprob2?a=5LTgWx8eTT9&S=stamps
一个简单的DP题。
F[i] = min(F[i], F[i-value[j]]+1)
F[i]表示凑出i所需的最少钱的张数,若F[i]>n,自然就不能用n张钱凑出i来了。
最后的结果就是i的最大值了。
/* ID:ay27272 PROG:stamps LANG:C++ */ #include <cstdio> #include <iostream> #include <cmath> #include <cstring> using namespace std; int f[2000005] = {0}; int value[55] = {0}; int main() { freopen("stamps.in","r",stdin); freopen("stamps.out","w",stdout); int n,m; scanf("%d%d", &n, &m); for (int i=0; i<m; i++) scanf("%d", &value[i]); memset(f, 100, sizeof(f)); f[0] = 0; int i=0; while (true) { i++; for (int j=0; j<m; j++) if (i-value[j]>=0) f[i] = min(f[i], f[i-value[j]]+1); if (f[i] > n) break; } printf("%d\n", i-1); return 0; }