1. 程式人生 > >BZOJ 1531: [POI2005]Bank notes 多重揹包

BZOJ 1531: [POI2005]Bank notes 多重揹包

1531: [POI2005]Bank notes

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 506  Solved: 279
[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

Sample Output

3

裸多重揹包

然鵝。。。BJ不知道WA多少發

先是發現更新揹包dp值時沒判容量大於物體致RE

後來又發現二進位制拆分的時候竟然鬼畜的判了一個& 真是傻爆了

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

typedef double db;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)x=-x,putchar('-');if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=3010,M=20100;

int n,K,val[N],v[N],f[M],b[N],c[N];

int main()
{
	n=read();
	register int i,j,k,tot=0;
	for(i=1;i<=n;++i)b[i]=read();
	for(i=1;i<=n;++i)c[i]=read();
	for(i=1;i<=n;++i)
	{
		for(k=0;(1<<k)<=c[i];++k)v[++tot]=b[i]<<k,val[tot]=(1<<k),c[i]-=(1<<k);
		if(c[i])v[++tot]=b[i]*c[i],val[tot]=c[i];
	}
	n=tot;
	memset(f,0X3f,sizeof(f));f[0]=0;
	K=read();
	for(i=1;i<=n;++i)
	{
		for(j=K;j>=v[i];--j)
		f[j]=min(f[j],f[j-v[i]]+val[i]);
	}
	print(f[K]);puts("");
	return 0;
}
/*
3
2 3 5
2 2 1
10

3
*/