POJ3260:The Fewest Coins(混合背包)
Description
Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus the number of coins he receives in change is minimized. Help him to determine what this minimum number is.
FJ wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1, V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1, C2 coins of value V2, ...., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).
Input
Line 2: N space-separated integers, respectively V 1, V 2, ..., VN coins ( V 1, ... VN)
Line 3: N space-separated integers, respectively C 1, C 2, ..., CN
Output
Sample Input
3 70 5 25 50 5 2 1
Sample Output
3
题意:给出钱币的方案数和总价值,然后给出每种钱币的价值与数量,而老板也是每种钱币都拥有,但是没有数量限制,购买东西的时候,价值超过给定价值的话,老板会找钱,要求最小的交流钱币的数量
思路:这题想了很久没有想出思路,虽然知道是背包,但是不知道该如何让运用,看了别人的代码,感觉人家的思路真心碉堡了,讲解在代码中
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int v[105],c[105],MAX,n,sum; int dp[33333],inf = 100000000; void ZeroOnePack(int cost,int cnt) { int i; for(i = sum+MAX; i>=cost; i--) dp[i] = min(dp[i],dp[i-cost]+cnt);//找出最小数量的方案 } void CompletePack(int cost,int cnt) { int i; for(i = sum+MAX+cost; i>=0; i--) dp[i] = min(dp[i],dp[i-cost]+cnt); } int MultiplePack() { int i,j,k; for(i = 1; i<=sum+MAX; i++) dp[i] = inf; dp[0] = 0;//dp数组用来记录钱币数量 for(i = 1; i<=2*n; i++) { if(i<=n)//这是顾客购买时所给的钱的数量 { k = 1; while(k<c[i]) { ZeroOnePack(k*v[i],k); c[i]-=k; k*=2; } ZeroOnePack(c[i]*v[i],c[i]); } else CompletePack(-v[i-n],1);//只所以是负数,是因为这是老板找钱的数目 } if(dp[sum]==inf) return -1; else return dp[sum]; } int main() { int i; while(~scanf("%d%d",&n,&sum)) { MAX = 0; for(i=1; i<=n; i++) { scanf("%d",&v[i]); MAX = max(MAX,v[i]); } MAX*=MAX;//保证背包足够大 for(i=1; i<=n; i++) scanf("%d",&c[i]); printf("%d\n",MultiplePack()); } return 0; }
相关文章
- Please ensure the argon2 header and library are installed
- ERROR: Could not find a version that satisfies the requirement six>=1.9.0
- C#:The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine
- log4j:WARN Please initialize the log4j system properly. 异常解决
- UVa699 The Falling Leaves
- HDU 1556 Color the ball (数状数组)
- The Xamarin Live Player Unpacked
- Referenced file contains errors For more information, right click on the message in the Problems View and select "Show Details..."
- The user specified as a definer ('root'@'%') does not exist
- HDUOJ Let the Balloon Rise 1004
- Linux pip 安装模块时,一直黄字错误:Could not find a version that satisfies the requirement
- 成功解决:Can‘t find Python executable “python“, you can set the PYTHON env variable.
- Learn Jenkins the hard way (3) - Jenkins的存储模型
- Runner.app/Info.plist does not exist. The Flutter “Thin Binary“ build phase must run after “Copy Bun
- error hawk@0.10.2: The engine “node“ is incompatible with this module. Expected versi
- Access restriction: The type JPEGImageEncoder is not accessible due to restriction
- fatal: unable to access ‘https://.../..‘: gnutls_handshake() failed: The TLS connection was n