当前位置:网站首页>POJ 2392 Space Elevator
POJ 2392 Space Elevator
2022-07-07 19:09:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
Multiple knapsack problem .
My backpack training question 3 , Multiple backpack .
It seems that I understand multiple backpacks a little .
My understanding of backpack nine is about multiple backpacks :
When something Volume * Number When it exceeds the capacity of the backpack , This makes a complete backpack ( Equivalent to infinite access )
void completepack(int h,int cost,int a)
{
for(int i=cost;i<=a;i++)
dp[i]=max(dp[i],dp[i-cost]+h);
}When there are more than one item but it can't fill the backpack , Do it one by one 01 knapsack Too wasteful . Then take the binary method , Every time I take 2.
Multiply every time 2 Of Do it once 01 knapsack .
In this way, the time complexity is reduced .
void zeroonepack(int h,int cost,int a)
{
for(int i=a;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+h);
}Separate like this and then decompose . Making multiple backpacks easy .
void multiplepack(int h,int cost,int c,int a)
{
if(cost*c>=a)
{
completepack(h,cost,a);
return;
// It's equivalent to making a complete backpack
}
int k=1;
while(k<c)
{
zeroonepack(h*k,cost*k,a);
c-=k;
k*=2;
// many times 01 knapsack
}
zeroonepack(h*c,cost*c,a);
}AC Code :
#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);
}
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116608.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢

10 schemes to ensure interface data security

虚拟数字人里的生意经

【Unity Shader】插入Pass实现模型遮挡X光透视效果

Simple configuration of single arm routing and layer 3 switching

Policy mode - unity

Nat address translation

ES6 note 1

面试唯品会实习测试岗、抖音实习测试岗【真实投稿】

NAT地址转换

Calculation of torque target value (ftorque) in servo torque control mode
随机推荐
The highest level of anonymity in C language
2022年推荐免费在线接收短信平台(国内、国外)
【剑指 Offer】59 - I. 滑动窗口的最大值
[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
Flipping Game(枚举)
2022.07.02
A hodgepodge of ICER knowledge points (attached with a large number of topics, which are constantly being updated)
基于图像和激光的多模态点云融合与视觉定位
Static routing configuration
Basic operation of chain binary tree (implemented in C language)
Redis
3. About cookies
Tsinghua, Cambridge and UIC jointly launched the first Chinese fact verification data set: evidence-based, covering many fields such as medical society
咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
强化学习-学习笔记8 | Q-learning
GSAP animation library
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
SlashData开发者工具榜首等你而定!!!
Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products