当前位置:网站首页>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
边栏推荐
- Yunjing network technology interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
- 3.关于cookie
- Do you know all four common cache modes?
- "Decryption" Huawei machine vision Corps: Huawei is moving up and the industry is moving forward
- Redis的发布与订阅
- SD_ DATA_ SEND_ SHIFT_ REGISTER
- 高考填志愿规则
- unity2d的Rigidbody2D的MovePosition函数移动时人物或屏幕抖动问题解决
- 如何选择合适的自动化测试工具?
- Desci: is decentralized science the new trend of Web3.0?
猜你喜欢
Reinforcement learning - learning notes 8 | Q-learning
Will low code help enterprises' digital transformation make programmers unemployed?
CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
标准ACL与扩展ACL
Datasimba launched wechat applet, and datanuza accepted the test of the whole scene| StartDT Hackathon
Introduction de l'API commune de programmation de socket et mise en œuvre de socket, select, Poll et epoll
NAT地址转换
go语言的字符串类型、常量类型和容器类型
嵌入式C语言程序调试和宏使用的技巧
Some key points in the analysis of spot Silver
随机推荐
Redis publishing and subscription
RISCV64
[paper sharing] where's crypto?
Classification of regression tests
6. About JWT
[论文分享] Where’s Crypto?
线程池中的线程工厂
小程序中实现付款功能
PHP面试题 foreach($arr as &$value)与foreach($arr as $value)的用法
【塔望方法论】塔望3W消费战略 - U&A研究法
Discuss | frankly, why is it difficult to implement industrial AR applications?
DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon
来了!GaussDB(for Cassandra)新特性亮相
App capture of charles+postern
如何选择合适的自动化测试工具?
学习open62541 --- [67] 添加自定义Enum并显示名字
Improve application security through nonce field of play integrity API
Tsinghua, Cambridge and UIC jointly launched the first Chinese fact verification data set: evidence-based, covering many fields such as medical society
SD_ DATA_ SEND_ SHIFT_ REGISTER
[C language] string function