poj2392 Space Elevator
来源:互联网 发布:oracle drop数据恢复 编辑:程序博客网 时间:2024/05/22 15:36
http://poj.org/problem?id=2392
1.多重背包思路
#include<cstdio>#include<cstring>
#include<algorithm>
using namespace std;
const int N=40005;
int dp[N+1];
struct blocks {
int h,a,num;
bool operator < (const blocks &m)const {
return this->a < m.a;
}
} b[405];
int main() {
int n,sum=0;
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d%d%d",&b[i].h,&b[i].a,&b[i].num);
sort(b,b+n);
int i,j,k;
memset(dp,0,sizeof(dp));
for(i=0; i<n; i++) {
int cnt=b[i].num;
for(k=1; cnt>0; k<<=1) {
int mul=min(k,cnt);
for(j=b[i].a; j>=mul*b[i].h; j--) {
dp[j]=max(dp[j],dp[j-mul*b[i].h]+mul*b[i].h);
}
cnt-=mul;
}
}
int ans=0;
for(i=0; i<=b[n-1].a; i++)
ans=max(ans,dp[i]);
printf("%d\n",ans);
return 0;
}
2.完全背包思路
using namespacestd;
const int N=40005;
int dp[N+1];
struct blocks {
int h,a,num;
bool operator < (const blocks &m)const {
return this->a < m.a;
}
} b[405];
int main() {
int n,sum=0;
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d%d%d",&b[i].h,&b[i].a,&b[i].num);
sort(b,b+n);
int i,j,k;
memset(dp,0,sizeof(dp));
for(i=0; i<n; i++) {
for(k=1; k<=b[i].num; k++) {
for(j=b[i].a; j>=b[i].h; j--) {
dp[j]=max(dp[j],dp[j-b[i].h]+b[i].h);
}
}
}
int ans=0;
for(i=0; i<=b[n-1].a; i++)
ans=max(ans,dp[i]);
printf("%d\n",ans);
return 0;
}
实际上完全背包问题和多重背包问题都可以由01背包思想变化而来
- poj2392 Space Elevator
- poj2392 - Space Elevator
- 【POJ2392】Space Elevator
- POJ2392:Space Elevator
- poj2392 Space Elevator
- [POJ2392]Space Elevator
- POJ2392-Space Elevator
- poj2392 Space Elevator
- poj2392(Space Elevator + 多重背包)
- POJ2392:Space Elevator(多重背包)
- poj2392 Space Elevator(多重背包)
- POJ2392 Space Elevator 动态规划
- 【Poj2392 Space Elevator】多重背包
- POJ2392-Space Elevator-多重背包
- poj2392 Space Elevator(多重背包)
- 【第三弹】【POJ2392】【Space Elevator】【解法一】
- POJ2392 Space Elevator (多重背包)
- POJ2392——Space Elevator(DP)
- 【数据结构】筛选法建堆
- day1.3 点线面
- JavaScript中的内置对象
- 地狱飞龙 【simpson 求积分】
- .net 获取json前端数据
- poj2392 Space Elevator
- 解析
- Java 多线程安全之 Atomic
- OpenCV直方图(2)
- 算法设计与分析(14)-- Valid Parentheses(难度:Easy)
- 二、RxJava基础 ---转化操作(Transforming Observables)
- The path to the driver executable must be set by the webdriver.gecko.driver system property
- 操作符*或者->的重载
- Android 线性布局参数