Test instructions: Given the 4 kinds of coins on the body, respectively, 1, 5, 10, 25 denominations have more than one, requires the formation of the denomination p coins as much as possible. Outputs the respective number of 4 coins that comprise p.
Idea: multiple backpacks, 300+ms. Use the 01 Backpack + binary method. Record the number of all coins and the number of 4 coins, and note the differences between the initialized DP arrays.
1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineLL Long Long5 using namespacestd;6 7 Const intn=10010, inf=-1;8 intcoin[4]={1,5,Ten, -};9 intdp[n][5], num[5];Ten intp; One A voidcal () - { -Memset (DP,0,sizeof(DP)); the for(intI=1; i<=p; i++) dp[i][0]=INF; - for(intI=0; i<4; i++) - { - intk=1, tmp=Num[i]; + while(1) - { + if(k>tmp&&tmp) k=tmp; A Else if(k>tmp) Break; at - for(intJ=p; j>=k*coin[i]; j--) - { - if(dp[j-k*coin[i]][0]!=inf &&dp[j][0]<dp[j-k*coin[i]][0]+k) - { -dp[j][0]=dp[j-k*coin[i]][0]+K; in for(intC=1; c<=i+1; C + +)//Number of copies -dp[j][c]=dp[j-k*Coin[i]] [C]; todp[j][i+1]+=k;//Number of Additions + } - } thetmp-=K; *k<<=1; $ }Panax Notoginseng } - if(dp[p][0]==inf) printf ("Charlie cannot buy coffee.\n"); the Elseprintf"Throw in%d cents,%d nickels,%d dimes, and%d quarters.\n", dp[p][1], dp[p][2], dp[p][3], dp[p][4] ); + } A the + intMain () - { $ $ //freopen ("Input.txt", "R", stdin); - while(cin>>p,p) - { the intCnt=0; - for(intI=0; i<4; i++)Wuyi { thescanf"%d", &num[i]); -cnt+=num[i]*Coin[i]; Wu } - if(cnt<p) About { $printf"Charlie cannot buy coffee.\n"); - Continue; - } - cal (); A } + return 0; the}
AC Code
POJ Charlie ' s change Charlie's conversion (multiple backpack, micro-variant)