当前位置:网站首页>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
边栏推荐
- Charles+Postern的APP抓包
- 【Unity Shader】插入Pass实现模型遮挡X光透视效果
- Sports Federation: resume offline sports events in a safe and orderly manner, and strive to do everything possible for domestic events
- Thread pool and singleton mode and file operation
- Mathematical analysis_ Notes_ Chapter 11: Fourier series
- Basic operation of chain binary tree (implemented in C language)
- How many times is PTA 1101 B than a
- 嵌入式C语言程序调试和宏使用的技巧
- Reuse of data validation framework Apache bval
- Tsinghua, Cambridge and UIC jointly launched the first Chinese fact verification data set: evidence-based, covering many fields such as medical society
猜你喜欢
Simple configuration of single arm routing and layer 3 switching
The performance and efficiency of the model that can do three segmentation tasks at the same time is better than maskformer! Meta & UIUC proposes a general segmentation model with better performance t
清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
如何选择合适的自动化测试工具?
低代码助力企业数字化转型会让程序员失业?
Comparison and selection of kubernetes Devops CD Tools
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%
Continuous test (CT) practical experience sharing
Nunjuks template engine
Redis
随机推荐
线程池的拒绝策略
Recommend free online SMS receiving platform in 2022 (domestic and foreign)
如何给“不卖笔”的晨光估值?
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
将模型的记忆保存下来!Meta&UC Berkeley提出MeMViT,建模时间支持比现有模型长30倍,计算量仅增加4.5%...
云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
A few simple steps to teach you how to see the K-line diagram
nest.js入门之 database
C语言中匿名的最高境界
Sports Federation: resume offline sports events in a safe and orderly manner, and strive to do everything possible for domestic events
云景网络科技面试题【杭州多测师】【杭州多测师_王sir】
PTA 1101 B是A的多少倍
企业展厅设计中常用的三种多媒体技术形式
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%
Redis publishing and subscription
gsap动画库
Wechat web debugging 8.0.19 replace the X5 kernel with xweb, so the X5 debugging method can no longer be used. Now there is a solution
3.关于cookie
国内的软件测试会受到偏见吗
[C language] string function