poj2392 Space Elevator(多重背包)
http://poj.org/problem?id=2392
题意:
有一群牛要上太空。他们计划建一个太空梯-----用一些石头垒。他们有K种不同类型的石头,每一种石头的高度为h_i,数量为c_i,并且由于会受到太空辐射,每一种石头不能超过这种石头的最大建造高度a_i。帮助这群牛建造一个最高的太空梯。
吐槽:
做练习的时候,连需不需要对数据排序都没分析清楚。。。下次再也不把练习安排在上午了,一般我上午状态极差(┬_┬)
这题由于数据较水,可以直接把多重背包转化为01背包求解,当然由于设定的状态不一样,写法也会不一样滴。
Code1(01背包):
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn= 410;
int dp[40010];
struct node {
int h, a, c;
bool operator <(const node& rhs) const {
return a< rhs.a;
}
};
node f[maxn]; int main()
{
int k, n, i, j, ans, maxa;
while(~scanf("%d",&n)) {
for(i=1; i<=n; i++) {
scanf("%d%d%d",&f[i].h,&f[i].a,&f[i].c);
}
sort(f+1,f+1+n);
memset(dp,0,sizeof(dp));
for(i=1; i<=n; i++) {
for(k=f[i].c; k>=1; k--){
for(j=f[i].a; j>=f[i].h; j--)
dp[j] =max(dp[j], dp[j-f[i].h]+f[i].h);
}
}
maxa = 0;
for(j=f[n].a; j>=0; j--)
if(maxa <dp[j]) maxa = dp[j];
printf("%d\n",maxa);
}
return 0;
}
Code(多重背包):
#include <cstdio>
#include <cstring>
#include <algorithm>
#define max(a,b) a>b ? a:b
using namespace std;
const int maxn= 410;
int dp[40010];
struct node {
int h, a, c;
bool operator <(const node& rhs) const {
return a< rhs.a;
}
};
node f[maxn];
void CompletePack(int C, int W, int V)
{
for(int i=C; i<=V; i++)
dp[i] = max(dp[i],dp[i-C]+W);
}
void ZeroOnePack(int C, int W, int V)
{
for(int i=V; i>=C; i--)
dp[i] = max(dp[i], dp[i-C]+W);
}
void MultipliePack(int C, int W, int M, int V)
{
if(C*M >=V) {
CompletePack(C,W,V);
return ;
}
int k=1;
while(k<M) {
ZeroOnePack(k*C,k*W,V);
M -=k;
k <<=1;
}
ZeroOnePack(C*M,W*M,V);
}
int main()
{
int k, n, i, j, ans, maxH;
while(~scanf("%d",&n)) {
for(i=0; i<n; i++) {
scanf("%d%d%d",&f[i].h,&f[i].a,&f[i].c);
}
sort(f,f+n);
memset(dp,0,sizeof(dp));
for(i=0; i<n; i++) {
MultipliePack(f[i].h,f[i].h,f[i].c,f[i].a);
}
maxH = 0;
for(i=f[n-1].a; i>=0; i--)
if(maxH < dp[i]) maxH = dp[i];
printf("%d\n",maxH);
}
return 0;
}
poj2392 Space Elevator(多重背包)的更多相关文章
- POJ 2392 Space Elevator(多重背包变形)
Q: 额外添加了最大高度限制, 需要根据 alt 对数据进行预处理么? A: 是的, 需要根据 alt 对数组排序 Description The cows are going to space! T ...
- poj 2392 Space Elevator(多重背包+先排序)
Description The cows are going to space! They plan to achieve orbit by building a sort of space elev ...
- poj2392 Space Elevator(多重背包问题)
Space Elevator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8569 Accepted: 4052 ...
- POJ2392 Space Elevator
题目:http://poj.org/problem?id=2392 一定要先按高度限制由小到大排序! 不然就相当于指定了一个累加的顺序,在顺序中是不能做到“只放后面的不放前面的”这一点的! 数组是四十 ...
- POJ 2392 Space Elevator(贪心+多重背包)
POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...
- POJ 2392 Space Elevator(多重背包)
显然塔的总高度不会超过最大的a[i],而a[i]之前的可以到达的高度 是由a值更小的块组成,所以按照a从小到大的顺序去转移. 然后就是多重背包判断存在性了,几乎和coin那题一样. 数据没coin丧病 ...
- poj2392 多重背包
//Accepted 868 KB 188 ms //多重背包 #include <cstdio> #include <cstring> #include <iostre ...
- POJ 2392 Space Elevator 背包题解
多重背包.本题不须要二分优化.相对简单点.由于反复数十分小,小于10. 而添加一个限制每种材料的高度做法.假设使用逆向填表,那么仅仅须要从这个高度往小递归填表就能够了. 还有就是注意要排序,以限制高度 ...
- Space Elevator [POJ2392] [DP][优化]
题目大意 n件物品,第i件hi高,有ci件,最高的一件不能超过ai的高度.问最高能堆多高 输入: 第一行,一个n 接下来每一行,为hi,ai,ci 输出,最高堆多高 样例输入: 37 40 35 23 ...
随机推荐
- LeeCode-Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- HDOJ-1018 Big Number
http://acm.hdu.edu.cn/showproblem.php?pid=1018 题意:给出一个数n,输出n的阶乘的位数 汗Σ( ° △ °|||)︴刚开始还准备上大数乘法 然而10000 ...
- 哥德尔,图灵和康托尔 part 1 哥德尔编号
在看计算理论相关的书的时候,偶然看到这个blog,http://skibinsky.com/godel-turing-and-cantor-the-math/,写的很好.我觉得用自动机的方式讲计算理论 ...
- 清除mac上安装软件的用户信息
有时候在mac系统上安装了一些软件后,尽管你将该软件卸载之后,可是原来的登录信息依然存在, 那么你就可以到下面的这个目录中查看一下,是否残留有信息文件.
- 【考虑周全+数学变形】【11月赛】Is it a fantastic matrix?
Is it a fantastic matrix? Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/ ...
- CentOS 7.0 systemd代替service
CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理.systemd兼容SysV和Linux标准 ...
- Oracle Golden Gate - 概念和机制 (ogg)
Golden Gate(简称OGG)提供异构环境下交易数据的实时捕捉.变换.投递. OGG支持的异构环境有: OGG的特性: 对生产系统影响小:实时读取交易日志,以低资源占用实现大交易量数据实时复制 ...
- RocketMQ与Kafka对比(18项差异)评价版
此文是rocketmq作者vintage.wang所写,对于每项对比,后面都增加了我的观点,有不对的地方,请各位指出. 淘宝内部的交易系统使用了淘宝自主研发的Notify消息中间件,使用Mysql作为 ...
- AAC ADTS解析
1.ADTS ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式. 一般的AAC解码器都需要把AAC的ES流打包成ADTS的格式,一般是在AAC ...
- cover letter issues
All cover letters should: Explain why you are sending a resume. Don't send a resume without a cover ...