当前位置:网站首页>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
边栏推荐
- Redis集群与扩展
- Reinforcement learning - learning notes 8 | Q-learning
- I feel cheated. Wechat tests the function of "size number" internally, and two wechat can be registered with the same mobile number
- cmd命令进入MySQL时报服务名或者命令错误(傻瓜式教学)
- AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
- 【剑指 Offer】59 - I. 滑动窗口的最大值
- How to implement safety practice in software development stage
- UVALive – 4621 Cav 贪心 + 分析「建议收藏」
- Will low code help enterprises' digital transformation make programmers unemployed?
- Redis的发布与订阅
猜你喜欢
Short selling, overprinting and stock keeping, Oriental selection actually sold 2.66 million books in Tiktok in one month
Review of network attack and defense
我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信
Do you know all four common cache modes?
Charles+Postern的APP抓包
Reinforcement learning - learning notes 8 | Q-learning
Antisamy: a solution against XSS attack tutorial
RIP和OSPF的区别和配置命令
高温火烧浑不怕,钟薛高想留清白在人间
99% of people don't know that privatized deployment is also a permanently free instant messaging software!
随机推荐
线程池中的线程工厂
UVALive – 4621 Cav 贪心 + 分析「建议收藏」
Nat address translation
学习open62541 --- [67] 添加自定义Enum并显示名字
Basic operation of chain binary tree (implemented in C language)
【HDU】5248-序列变换(贪心+二分)「建议收藏」
How much does it cost to develop a small program mall?
RISCV64
微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案
ES6 note 1
Idea completely uninstalls installation and configuration notes
Skills of embedded C language program debugging and macro use
[information security laws and regulations] review
Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)
Multimodal point cloud fusion and visual location based on image and laser
Sports Federation: resume offline sports events in a safe and orderly manner, and strive to do everything possible for domestic events
App capture of charles+drony
testing and SQA_动态白盒測试[通俗易懂]
体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
In 2021, the national average salary was released. Have you reached the standard?