当前位置:网站首页>POJ 2392 Space Elevator
POJ 2392 Space Elevator
2022-07-07 16:54:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
多重背包问题。
我的背包训练第三题,多重背包。
似乎有点理解多重背包了。
我对背包九讲多重背包的理解:
当某件物品 体积*数量 超过背包的容积的时候,这就做全然背包(相当于无限取)
void completepack(int h,int cost,int a)
{
for(int i=cost;i<=a;i++)
dp[i]=max(dp[i],dp[i-cost]+h);
}而某件物品有多件却不能装满背包的时候,一件一件的来做01背包 太浪费。然后採取二进制的办法,每次乘2。
把每次乘2 的 来做一次01 背包。
这样时间复杂度减少 。
void zeroonepack(int h,int cost,int a)
{
for(int i=a;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+h);
}这样分开然后再分解。让多重背包就简单起来了。
void multiplepack(int h,int cost,int c,int a)
{
if(cost*c>=a)
{
completepack(h,cost,a);
return;
//相当于做一次全然背包
}
int k=1;
while(k<c)
{
zeroonepack(h*k,cost*k,a);
c-=k;
k*=2;
//多次01背包
}
zeroonepack(h*c,cost*c,a);
}AC 代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long long
using namespace std;
int dp[40001];
int n;
struct lx
{
int h,c,a;
}l[401];
bool cmp(lx a,lx b)
{
return a.a<b.a;
}
void zeroonepack(int h,int cost,int a)
{
for(int i=a;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+h);
}
void completepack(int h,int cost,int a)
{
for(int i=cost;i<=a;i++)
dp[i]=max(dp[i],dp[i-cost]+h);
}
void multiplepack(int h,int cost,int c,int a)
{
if(cost*c>=a)
{
completepack(h,cost,a);
return;
}
int k=1;
while(k<c)
{
zeroonepack(h*k,cost*k,a);
c-=k;
k*=2;
}
zeroonepack(h*c,cost*c,a);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int m=0;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&l[i].h,&l[i].a,&l[i].c);
m=max(m,l[i].a);
}
memset(dp,0,sizeof(dp));
sort(l,l+n,cmp);
for(int i=0;i<n;i++)
{
multiplepack(l[i].h,l[i].h,l[i].c,l[i].a);
}
int ans=0;
for(int i=0;i<=m;i++)
//printf("%d =\n",dp[i]);
ans=max(ans,dp[i]);
printf("%d\n",ans);
}
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116608.html原文链接:https://javaforall.cn
边栏推荐
- 小试牛刀之NunJucks模板引擎
- Improve application security through nonce field of play integrity API
- 【剑指 Offer】59 - I. 滑动窗口的最大值
- 体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
- 數據驗證框架 Apache BVal 再使用
- Realize payment function in applet
- Summary of evaluation indicators and important knowledge points of regression problems
- 高考填志愿规则
- Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)
- Redis cluster and expansion
猜你喜欢

CVPR 2022丨学习用于小样本语义分割的非目标知识

Learn to make dynamic line chart in 3 minutes!

Short selling, overprinting and stock keeping, Oriental selection actually sold 2.66 million books in Tiktok in one month

小程序中实现付款功能

Kirk Borne的本周学习资源精选【点击标题直接下载】

RIP和OSPF的区别和配置命令

嵌入式C语言程序调试和宏使用的技巧

50亿,福建又诞生一只母基金

Hash, bitmap and bloom filter for mass data De duplication
![[software test] from the direct employment of the boss of the enterprise version, looking at the resume, there is a reason why you are not covered](/img/73/cbbe82fd6bdfa8177f5bfcf683010d.jpg)
[software test] from the direct employment of the boss of the enterprise version, looking at the resume, there is a reason why you are not covered
随机推荐
抢占周杰伦
Realize payment function in applet
Some key points in the analysis of spot Silver
unity2d的Rigidbody2D的MovePosition函数移动时人物或屏幕抖动问题解决
Hutool - lightweight DB operation solution
NAT地址转换
Redis publishing and subscription
nest. Database for getting started with JS
清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
[software test] from the direct employment of the boss of the enterprise version, looking at the resume, there is a reason why you are not covered
Save the memory of the model! Meta & UC Berkeley proposed memvit. The modeling time support is 30 times longer than the existing model, and the calculation amount is only increased by 4.5%
PIP related commands
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
DeSci:去中心化科学是Web3.0的新趋势?
Redis cluster and expansion
GSAP animation library
Reuse of data validation framework Apache bval
Interview vipshop internship testing post, Tiktok internship testing post [true submission]
Reject policy of thread pool
Embedded interview questions (algorithm part)