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

[ L i n k \frak{Link} Link]


把朴素多重背包的式子稍作转化,得到:
f j = m a x { f k ′ − k ′ v a l u e    ∣    k ′ ∈ [ j ′ − c o u n t i , j ′ ] } + j ′ v a l u e ,    j ′ c o s t i + R e m a i n d e r = j \mathcal{f_{j}=max\{f_{k'}-k'value\;|\;k'\in[j'-count_i,j']\}+j'value,\;j'cost_i+Remainder=j} fj=max{fkkvaluek[jcounti,j]}+jvalue,jcosti+Remainder=j(枚举 R e m a i n d e r \mathcal{Remainder} Remainder
看了半天感觉不太对结果发现这道题也是代价等于价值,,


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
int n, m;
int a[205];
int c[205];
pair<int, int> q[20005];
int qh, qt;
int f[20005];
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
	}
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &c[i]);
	}
	scanf("%d", &m);
	memset(f, 0x3f, sizeof(f));
	f[0] = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j < a[i]; ++j) {
			qh = 1;
			qt = 0;
			for (int x, k = 0; ; ++k) {
				if ((x = k * a[i] + j) > m) break;
				while (qh <= qt && q[qh].second < k - c[i]) {
					++qh;
				}
				while (qh <= qt && q[qt].first >= f[x] - k) {
					--qt;
				}
				q[++qt] = make_pair(f[x] - k, k);
				f[x] = min(f[x], q[qh].first + k);
			}
		}
	}
	printf("%d", f[m]);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值