标签:strong win 硬币 string ges freopen through inline amp
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).
农夫John想到镇上买些补给。为了高效地完成任务,他想使硬币的转手次数最少。即使他交付的硬 币数与找零得到的的硬币数最少。 John想要买T(1<=T<=10000)样东西(2017-7-20 管理员注:这个翻译有问题,实际为要买价值为T的东西)。有N(1<=n<=100)种货币参与流通,面值分别为V1,V2..Vn (1<=Vi<=120)。John有Ci个面值为Vi的硬币(0<=Ci<=10000)。我们假设店主有无限多的硬币, 并总按最优方案找零。
输入格式:
Line 1: Two space-separated integers: N and T.
Line 2: N space-separated integers, respectively V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ..., CN
输出格式:
Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1.
Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.
思路比较简单
对john做一次多重背包
对店主做一次完全背包(然而不会写代码)
多重背包用二进制优化
另外,本蒟蒻不怎么懂为什么枚举上界是所有面值相乘再加T,
刚开始写面值乘数量死活RE QWQ...
#include<cstring> #include<cstdio> #include<cstdlib> #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<22,stdin),p1==p2)?EOF:*p1++) #define min(a,b) (a<b?a:b) #define max(a,b) (a<b?b:a) char buf[1<<22],*p1=buf,*p2=buf; //#define int long long using namespace std; const int MAXN=5*1e6+10,INF=1e8+10; inline int read() { char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } int f[MAXN];//恰好为i时的最小花费 int g[MAXN];//完全背包 int val[MAXN],num[MAXN]; int N,T,limit=0; int main() { #ifdef WIN32 freopen("a.in","r",stdin); #endif N=read();T=read(); for(int i=1;i<=N;i++) val[i]=read(); for(int i=1;i<=N;i++) num[i]=read(),limit+=val[i]*val[i]; memset(f,0x3f,sizeof(f)); memset(g,0x3f,sizeof(g)); g[0]=0;f[0]=0; for(int i=1;i<=N;i++) for(int j=val[i];j<=limit;j++) g[j]=min(g[j],g[j-val[i]]+1); for(int i=1;i<=N;i++) { for(int k=1;k<=num[i];k<<=1) { for(int j=limit;j>=val[i]*k;j--) f[j]=min(f[j],f[j - val[i]*k]+k); num[i]-=k; } if(num[i]) for(int j=limit;j>=val[i]*num[i];j--) f[j]=min(f[j],f[j - val[i]*num[i]]+num[i]); } int ans=INF; for(int i=T;i<=limit;i++) ans=min(ans,f[i]+g[i-T]); ans==INF?printf("-1"):printf("%d",ans); return 0; }
洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)
标签:strong win 硬币 string ges freopen through inline amp
原文地址:https://www.cnblogs.com/zwfymqz/p/8782645.html