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(多重背包)的更多相关文章

  1. POJ 2392 Space Elevator(多重背包变形)

    Q: 额外添加了最大高度限制, 需要根据 alt 对数据进行预处理么? A: 是的, 需要根据 alt 对数组排序 Description The cows are going to space! T ...

  2. poj 2392 Space Elevator(多重背包+先排序)

    Description The cows are going to space! They plan to achieve orbit by building a sort of space elev ...

  3. poj2392 Space Elevator(多重背包问题)

    Space Elevator   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8569   Accepted: 4052 ...

  4. POJ2392 Space Elevator

    题目:http://poj.org/problem?id=2392 一定要先按高度限制由小到大排序! 不然就相当于指定了一个累加的顺序,在顺序中是不能做到“只放后面的不放前面的”这一点的! 数组是四十 ...

  5. POJ 2392 Space Elevator(贪心+多重背包)

    POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...

  6. POJ 2392 Space Elevator(多重背包)

    显然塔的总高度不会超过最大的a[i],而a[i]之前的可以到达的高度 是由a值更小的块组成,所以按照a从小到大的顺序去转移. 然后就是多重背包判断存在性了,几乎和coin那题一样. 数据没coin丧病 ...

  7. poj2392 多重背包

    //Accepted 868 KB 188 ms //多重背包 #include <cstdio> #include <cstring> #include <iostre ...

  8. POJ 2392 Space Elevator 背包题解

    多重背包.本题不须要二分优化.相对简单点.由于反复数十分小,小于10. 而添加一个限制每种材料的高度做法.假设使用逆向填表,那么仅仅须要从这个高度往小递归填表就能够了. 还有就是注意要排序,以限制高度 ...

  9. Space Elevator [POJ2392] [DP][优化]

    题目大意 n件物品,第i件hi高,有ci件,最高的一件不能超过ai的高度.问最高能堆多高 输入: 第一行,一个n 接下来每一行,为hi,ai,ci 输出,最高堆多高 样例输入: 37 40 35 23 ...

随机推荐

  1. 2.x ESL第二章习题 2.8

    题目 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3 ...

  2. mysql的主从复制配置

    怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作:  1.1.版本一致  1.2.初始化表,并在后台启动mysql  1.3.修改root的密码 2.修 ...

  3. CF 338 D GCD Table(CRT)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 给定一个序列,a[1 ..k],问是否存在(i , ...

  4. AliCTF 2016

    上上周参加了阿里的CTF,靠着最后绝杀队伍有幸拿到了国内第一名,也顺利进入了XCTF Final.把自己做的几个题简单写了下,发出来也算个总结吧. PWN-FB 经典的null byte overfl ...

  5. Flashback Version/Transaction Query

    1.应用Flashback Version Query查询记修改版本 SQL> select dbms_flashback.get_system_change_number from dual; ...

  6. 1. 用U盘安装Centos6.5 + Win7 双系统

    一. 用U盘安装Centos6.5 + Win7 双系统 准备工作:U盘(8G).需要安装的Centos6.5系统(64bit).EasyBCD(用来修复引导,否则开机只有一个系统).         ...

  7. 面试前的准备---C#知识点回顾----01

    过完年来,准备找份新工作,虽然手里的工作不错,但树挪死,人挪活.咱不能一直在一个坑里生活一辈子,外面的世界毕竟是很美好的. 为了能正常的找到自己中意的工作,最近是将所有的基础知识拿出来复习了一次.仅作 ...

  8. RFC端口号定义

    RFC关于计算机端口号定义 http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers. ...

  9. php Debugging with Xdebug and Sublime Text 3(转)

    Debugging – we all do it a lot. Writing code perfectly the first time around is hard and only a few ...

  10. 百度地图坐标转换API和地图API

    利用百度地图的服务将经纬度转换为米单位坐标 using System; using System.Collections.Generic; using System.Linq; using Syste ...