当前位置:网站首页>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
边栏推荐
- 【Unity Shader】插入Pass实现模型遮挡X光透视效果
- 能同时做三个分割任务的模型,性能和效率优于MaskFormer!Meta&UIUC提出通用分割模型,性能优于任务特定模型!开源!...
- Wireshark分析抓包数据*.cap
- 链式二叉树的基本操作(C语言实现)
- Continuous test (CT) practical experience sharing
- [Blue Bridge Cup training 100 questions] sort scratch from small to large. Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question
- Tips for short-term operation of spot silver that cannot be ignored
- [demo] circular queue and conditional lock realize the communication between goroutines
- Comparison and selection of kubernetes Devops CD Tools
- CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
猜你喜欢

【软件测试】从企业版BOSS直聘,看求职简历,你没被面上是有原因的

Simple configuration of single arm routing and layer 3 switching

Wireshark分析抓包数据*.cap

Wireshark analyzes packet capture data * cap

持续测试(CT)实战经验分享

socket編程之常用api介紹與socket、select、poll、epoll高並發服務器模型代碼實現

Reinforcement learning - learning notes 8 | Q-learning

Redis cluster and expansion

Industry case | digital operation base helps the transformation of life insurance industry

The highest level of anonymity in C language
随机推荐
PTA 1102 teaching Super Champion volume
Kubernetes DevOps CD工具对比选型
Improve application security through nonce field of play integrity API
Basic concepts and properties of binary tree
简单几步教你如何看k线图图解
SQLite SQL exception near "with": syntax error
PHP面试题 foreach($arr as &$value)与foreach($arr as $value)的用法
sqlite sql 异常 near “with“: syntax error
抢占周杰伦
Recommend free online SMS receiving platform in 2022 (domestic and foreign)
CVPR 2022丨学习用于小样本语义分割的非目标知识
Discuss | what preparations should be made before ar application is launched?
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
小程序中实现付款功能
Tips for short-term operation of spot silver that cannot be ignored
Complete e-commerce system
【C语言】字符串函数
Realize payment function in applet
Introduction de l'API commune de programmation de socket et mise en œuvre de socket, select, Poll et epoll
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%