当前位置:网站首页>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
边栏推荐
- Discuss | frankly, why is it difficult to implement industrial AR applications?
- PIP related commands
- Thread pool and singleton mode and file operation
- 现货白银分析中的一些要点
- 微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案
- GSAP animation library
- Redis
- 云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
- Hutool - lightweight DB operation solution
- How many times is PTA 1101 B than a
猜你喜欢

企业展厅设计中常用的三种多媒体技术形式

Tsinghua, Cambridge and UIC jointly launched the first Chinese fact verification data set: evidence-based, covering many fields such as medical society

Hash, bitmap and bloom filter for mass data De duplication

Realize payment function in applet

App capture of charles+drony

String type, constant type and container type of go language

Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model

Classification of regression tests

Kubernetes DevOps CD工具对比选型

现货白银分析中的一些要点
随机推荐
GSAP animation library
【C语言】字符串函数
【Unity Shader】插入Pass实现模型遮挡X光透视效果
PTA 1102 教超冠军卷
Cloud security daily 220707: Cisco Expressway series and telepresence video communication server have found remote attack vulnerabilities and need to be upgraded as soon as possible
完整的电商系统
[paper sharing] where's crypto?
Tips for short-term operation of spot silver that cannot be ignored
Datasimba launched wechat applet, and datanuza accepted the test of the whole scene| StartDT Hackathon
Redis集群与扩展
Nunjuks template engine
Comparison and selection of kubernetes Devops CD Tools
Nat address translation
Mathematical analysis_ Notes_ Chapter 11: Fourier series
Will domestic software testing be biased
PTA 1101 B是A的多少倍
卖空、加印、保库存,东方甄选居然一个月在抖音卖了266万单书
CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
Calculation of torque target value (ftorque) in servo torque control mode
Basic concepts and properties of binary tree