/*
* 此題如果使用多重背包做的話,即使加上了二進制優化,效率也不是很高
* 此題使用完全背包本人覺得效率還可以,完全背包和多重背包在這狀態和狀態轉移方程是一樣的
* 不同的就是路徑記錄的處理
* If this question using multiple backpack to do, even with a binary optimization, efficiency is not very high
* This title is completely backpack I think that efficiency can also completely backpack and multiple backpack in this state and the state transition equation is the same
* Different path processing of records
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define COINTYPE 4
#define MAXN 20001
int dp[MAXN], coin_cnt[COINTYPE*20], used[MAXN];
int coin_val[] = { 1, 5, 10, 25 }, path[MAXN];
void dynamic_programming(int max_v)
{
memset(dp, 0, sizeof(dp));
memset(path, -1, sizeof(path));
dp[0] = 1;
for(int i = 0; i < COINTYPE; i ++) {
memset(used, 0, sizeof(used));
for(int j = coin_val[i]; j <= max_v; j ++) {
if( dp[j-coin_val[i]] &&
dp[j] < dp[j-coin_val[i]]+1 &&
used[j-coin_val[i]]+1 <= coin_cnt[i] ) {
dp[j] = dp[j-coin_val[i]]+1;
used[j] = used[j-coin_val[i]]+1;
path[j] = j-coin_val[i];
}
}
}
if( !dp[max_v] ) {
printf("Charlie cannot buy coffee.\n");
}
else {
memset(coin_cnt, 0, sizeof(coin_cnt));
while( -1 != path[max_v] ) {
coin_cnt[max_v-path[max_v]] ++;
max_v = path[max_v];
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",
coin_cnt[1], coin_cnt[5], coin_cnt[10], coin_cnt[25] );
}
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int max_v, flag;
while( true ) {
flag = 0;
scanf("%d", &max_v);
for(int i = 0; i < COINTYPE; i ++) {
scanf("%d", &coin_cnt[i]);
if( coin_cnt[i] ) {
flag = 1;
}
}
if( !flag && !max_v ) {
break;
}
dynamic_programming(max_v);
}
return 0;
}
poj_1787
最新推荐文章于 2018-11-01 23:38:27 发布