POJ 3260 The Fewest Coins(多重背包+完全背包—>交易数量最小)@
2016-10-26 21:49
423 查看
The Fewest Coins
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit Status
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 1: Two space-separated integers: N and T.
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
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.
Sample Input
Sample Output
Hint
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.
透过现象看本质,买家是多重背包,卖家是完全背包,求交易数量最小初始化为极大值,求交易数量最大初始化为最小值;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <map>
#include <vector>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 10010;
int dp1
, dp2
, used
;
int v
, num
;
int main()
{
int n, t;
while(scanf("%d %d", &n, &t)!=EOF)
{
for(int i=0;i<n;i++) scanf("%d", &v[i]);
for(int i=0;i<n;i++) scanf("%d", &num[i]);
for(int i=1;i<=N;i++) dp1[i]=dp2[i]=inf;
dp1[0]=dp2[0]=0;
for(int i=0;i<n;i++)
{
for(int j=v[i];j<=N;j++)
dp1[j]=min(dp1[j-v[i]]+1,dp1[j]);
}
for(int i=0;i<n;i++)
{
memset(used,0,sizeof(used));
for(int j=v[i];j<=N;j++)
{
if(dp2[j]>dp2[j-v[i]]+1&&used[j-v[i]]<num[i])
{
dp2[j]=dp2[j-v[i]]+1;
used[j]=used[j-v[i]]+1;
}
}
}
int ans=dp2[t];
for(int i=t+1;i<N;i++)
ans=min(ans,dp2[i]+dp1[i-t]);
if(ans>=inf) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit Status
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 1: Two space-separated integers: N and T.
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
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.
Sample Input
3 70 5 25 50 5 2 1
Sample Output
3
Hint
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.
透过现象看本质,买家是多重背包,卖家是完全背包,求交易数量最小初始化为极大值,求交易数量最大初始化为最小值;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <map>
#include <vector>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 10010;
int dp1
, dp2
, used
;
int v
, num
;
int main()
{
int n, t;
while(scanf("%d %d", &n, &t)!=EOF)
{
for(int i=0;i<n;i++) scanf("%d", &v[i]);
for(int i=0;i<n;i++) scanf("%d", &num[i]);
for(int i=1;i<=N;i++) dp1[i]=dp2[i]=inf;
dp1[0]=dp2[0]=0;
for(int i=0;i<n;i++)
{
for(int j=v[i];j<=N;j++)
dp1[j]=min(dp1[j-v[i]]+1,dp1[j]);
}
for(int i=0;i<n;i++)
{
memset(used,0,sizeof(used));
for(int j=v[i];j<=N;j++)
{
if(dp2[j]>dp2[j-v[i]]+1&&used[j-v[i]]<num[i])
{
dp2[j]=dp2[j-v[i]]+1;
used[j]=used[j-v[i]]+1;
}
}
}
int ans=dp2[t];
for(int i=t+1;i<N;i++)
ans=min(ans,dp2[i]+dp1[i-t]);
if(ans>=inf) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins (混合背包--多重背包+完全背包)
- POJ3260 The Fewest Coins ——完全背包+多重背包——Pku3260
- POJ 3260 The Fewest Coins (多重背包 + 完全背包)
- POJ 3260 The Fewest Coins 最少硬币个数(完全背包+多重背包,混合型)
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- poj 3260 The Fewest Coins(多重背包+完全背包)
- POJ3260_The Fewest Coins_deque优化dp解多重背包和完全背包
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 - The Fewest Coins(完全背包+多重背包)
- POJ3260——The Fewest Coins(多重背包+完全背包)
- poj 3260 The Fewest Coins (多重背包 + 完全背包)
- poj 3260 The Fewest Coins 多重背包+完全背包
- POJ - 3260 The Fewest Coins (完全背包)
- POJ 3260 The Fewest Coins(完全背包+多重背包(转化为01背包))
- 背包问题--POJ 3260 The Fewest Coins【完全背包+多重背包】
- POJ 3260 The Fewest Coins(完全背包 + 多重背包 + 单调队列优化)
- POJ 3260 The Fewest Coins 多重背包+完全背包
- poj 3260 The Fewest Coins(多重背包+完全背包)
- poj 3260 The Fewest Coins(背包)