bzoj1531: [POI2005]Bank notes(多重背包)
1531: [POI2005]Bank notes
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 521 Solved: 285
[Submit][Status][Discuss]
Description
Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出面值k求最少要用多少个硬币.
Input
第一行一个数 n, 1 <= n <= 200. 接下来一行 n 个整数b1, b2,..., bn, 1 <= b1 < b2 < ... < b n <= 20 000, 第三行 n 个整数c1, c2,..., cn, 1 <= ci <= 20 000, 表示每种硬币的个数.最后一行一个数k – 表示要凑的面值数量, 1 <= k <= 20 000.
Output
第一行一个数表示最少需要付的硬币数
Sample Input
3
2 3 5
2 2 1
10
2 3 5
2 2 1
10
Sample Output
3
题解:
二进制优化多重背包板子(我这都不会,我太弱啦!
网上找的一个比较美观的模板
#pragma GCC optimize("O2")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<limits.h>
#include<ctime>
#define N 100001
typedef long long ll;
const int inf=999999999;
const int maxn=2017;
using namespace std;
inline int read()
{
int f=1,x=0;char ch=getchar();
while(ch>'9'|ch<'0')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch<='9'&&ch>='0')
{
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return f*x;
}
int b[N],c[N],dp[N];
int main()
{
int n=read();
for(int i=1;i<=n;i++)
{
b[i]=read();
}
for(int i=1;i<=n;i++)
c[i]=read();
int m=read();
memset(dp,0x3f,sizeof(dp));
dp[0]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=c[i];j<<=1)
{
for(int k=m;k>=b[i]*j;k--)
dp[k]=min(dp[k],dp[k-b[i]*j]+j);c[i]-=j;
if(c[i])
for(int k=m;k>=b[i]*c[i];k--)
dp[k]=min(dp[k],dp[k-c[i]*b[i]]+c[i]);
}
}
printf("%d\n",dp[m]);
}
就让我永远不在这里写下什么有意义的话——by 吉林神犇 alone_wolf

浙公网安备 33010602011771号