【BZOJ1531】[POI2005]Bank notes【多重背包】

【题目链接】

照着论文打了一发,发现还挺好玩。

/* Pigonometry */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 20005;

int n, m, dp[maxn], w[maxn], c[maxn];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

int main() {
	n = iread();
	for(int i = 1; i <= n; i++) w[i] = iread();
	for(int i = 1; i <= n; i++) c[i] = iread();
	m = iread();

	memset(dp, 0x3f, sizeof(dp));
	dp[0] = 0;
	for(int i = 1; i <= n; i++)
		for(int t = 1; c[i]; t <<= 1) {
			t = min(t, c[i]);
			c[i] -= t;
			for(int j = m; j >= w[i] * t; j--)
				dp[j] = min(dp[j], dp[j - w[i] * t] + t);
		}

	printf("%d\n", dp[m]);
	return 0;
}


你可能感兴趣的:(【BZOJ1531】[POI2005]Bank notes【多重背包】)