poj 2392 Space Elevator dp 多重背包

题目

题目链接:http://poj.org/problem?id=2392

题目来源:《挑战》练习题

简要题意: K 种块,每种高度hi ci 个,顶部位置不能超过 ai 求搭起来的最大高度。

数据范围: 1K400;1hi100;1ci10;1ai40000

题解

实际上肯定是个多重背包咯。

ai 排序然后每轮以 ai 为容量去做多重背包就行了。

由于只要判断访问,可以使用一些poj 1742 题解这里面的优化技巧

实现

用普通的多重背包估计也是可以的,我没试过。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 405;
const int M = 4E4+5;

struct Node {
    int lim, cnt, v;
    bool operator<(const Node &o) const {
        return lim < o.lim;
    }
};

Node a[N];
bool dp[M];
int cnt[M];
int use[M];

int main()
{
    int n, from;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d%d%d", &a[i].v, &a[i].lim, &a[i].cnt);
    }
    sort(a+1, a+n+1);

    dp[0] = true;
    for (int i = 1; i <= n; i++) {
        for (int j = a[i].v; j <= a[i].lim; j++) {
            from = j-a[i].v;
            if (dp[from] && !dp[j] && (use[from] != i || cnt[from] < a[i].cnt)) {
                dp[j] = true;
                cnt[j] = use[from] == i ? cnt[from]+1 : 1;
                use[j] = i;
            }
        }
    }

    for (int i = a[n].lim; ~i; i--) {
        if (dp[i]) {
            printf("%d\n", i);
            break;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值