poj 2392 Space Elevator dp 多重背包

来源:互联网 发布:google tensorflow应用 编辑:程序博客网 时间:2024/05/29 03:21

题目

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

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

简要题意:K种块,每种高度hici个,顶部位置不能超过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 secondusing 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;}// headconst 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