codevs5429(单调队列优化多重背包)

其实这个知识点并不是很难。。只是觉得并不会这么极限卡log就没去管他。。直到今天差点被卡TAT

稍微推导一下

设d[i][j]为前i种物品体积为j的最大价值

d[i][j]=max\{d[i-1][j-kv_i]+kw_i\}

此时\small 0\leq k\leq min\{j/v_i,c[i]\}

j=pv_i+qk=p-k,有

\small d[i][j]=max\{d[i-1][q+(p-k)v_i]+kw_i\}=max\{d[i-1][q+kv_i]-kw_i\}+pw_i

此时\small p-min\{j/v_i,c[i]\}\leq k\leq p\small max\{0,p-c[i]\}\leq k\leq p

然后考虑到枚举k的时候k一定为整数,所以直接\small p-c[i]\leq k\leq p就行。。然后发现这就是划窗。。

所以枚举q后枚举k,然后直接单调队列维护最值即可。。

然后时间复杂度降成了O(nm)而且好像并不比二进制压缩复杂多少。。所以以后就写单调队列了。。

 

 

 

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#include<stdlib.h>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,1,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 7005 
#define nm 7005
#define pi 1.1415926535897931
const int inf=1e9+7;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
 



int n,m,c[NM],w[NM],b[NM],qh,qt,q[NM];
int d[NM],f[NM];



int main(){
    n=read();m=read();
    inc(i,1,n)c[i]=read(),w[i]=read(),b[i]=read();
    inc(i,1,n){
	inc(j,0,c[i]-1){
	    qh=1;qt=0;
	    for(int k=0;j+k*c[i]<=m;k++){
		while(qh<=qt&&q[qh]<k-b[i])qh++;
		while(qh<=qt&&f[j+q[qt]*c[i]]-q[qt]*w[i]<=f[j+k*c[i]]-k*w[i])qt--;
		q[++qt]=k;
		d[j+k*c[i]]=f[j+q[qh]*c[i]]-q[qh]*w[i]+k*w[i];
	    }
	}
	inc(i,1,m)f[i]=d[i];
    }
    return 0*printf("%d\n",d[m]);
}

 

 

 

 

5429 多重背包

 

时间限制: 1 s

空间限制: 256000 KB

题目等级 : 钻石 Diamond

题目描述 Description

你有一个容量为M的背包,和N种物品。

每种物品都有三个属性,vi,wi,与ci,分别表示这种物品的体积、价值和件数。

你的任务是,从这些所给物品中,选出若干件,其体积之和不能超过背包容量,并且使所选物品的权值的和最大。

输入描述 Input Description

第一行两个整数N,M

接下来N行每行三个数vi,wi,ci描述第i件物品的属性

输出描述 Output Description

最大的权值和

样例输入 Sample Input

2 8

2 100 4

4 100 2

样例输出 Sample Output

400

数据范围及提示 Data Size & Hint

对于20%的数据,ci=1

对于60%的数据,N,M<=500,ci<=100

对于90%的数据,N,M<=3000

对于100%的数据,N,M<=7000,ci<=5000,保证答案不超过2147483647

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值