当前位置:网站首页>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
边栏推荐
- 虚拟数字人里的生意经
- Golang client server login
- Differences between rip and OSPF and configuration commands
- [demo] circular queue and conditional lock realize the communication between goroutines
- Learn to make dynamic line chart in 3 minutes!
- 企业展厅设计中常用的三种多媒体技术形式
- GSAP animation library
- 现货白银分析中的一些要点
- Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
- Charles+Postern的APP抓包
猜你喜欢
How to choose the appropriate automated testing tools?
Redis
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
Antisamy: a solution against XSS attack tutorial
Charles+drony的APP抓包
强化学习-学习笔记8 | Q-learning
gsap动画库
Tips for short-term operation of spot silver that cannot be ignored
CVPR 2022丨学习用于小样本语义分割的非目标知识
Charles+Postern的APP抓包
随机推荐
Discuss | what preparations should be made before ar application is launched?
App capture of charles+postern
Cloud security daily 220707: Cisco Expressway series and telepresence video communication server have found remote attack vulnerabilities and need to be upgraded as soon as possible
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
DeSci:去中心化科学是Web3.0的新趋势?
Sports Federation: resume offline sports events in a safe and orderly manner, and strive to do everything possible for domestic events
PTA 1102 teaching Super Champion volume
6.关于jwt
简单几步教你如何看k线图图解
10 schemes to ensure interface data security
Basic operation of chain binary tree (implemented in C language)
C语言中匿名的最高境界
Classification of regression tests
静态路由配置
2022-07-04 matlab reads video frames and saves them
直播预约通道开启!解锁音视频应用快速上线的秘诀
基于图像和激光的多模态点云融合与视觉定位
Static routing configuration
I feel cheated. Wechat tests the function of "size number" internally, and two wechat can be registered with the same mobile number
[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