/*
translation:
有k种石块,每种石块有ci个,每种石块所在的高度不能超过ai,求用这k种石头所能达到的最高高度
solution:
多重背包dp可行性解法,贪心
先排序,然后按照多重背包dp可行性解法即可ac
note:
此题用常规的多重背包wa了,不知道为啥?
date:
2016.9.8
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 400 +5;
struct Stock {
int h, a, c; //石块的高度,高度上限,该类石块的数量
bool operator < (const Stock& rhs) const {
if(a == rhs.a) return h > rhs.h;
else return a < rhs.a;
}
} stocks[60000];
int k;
int dp[40000 + 5];
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d" ,&k)) {
int max_h = -1;
for(int i = 0; i < k; i++) {
scanf("%d%d%d", &stocks[i].h, &stocks[i].a, &stocks[i].c);
max_h = max(max_h, stocks[i].a);
}
sort(stocks, stocks + k);
memset(dp, -1, sizeof(dp));
dp[0] = 0;
for(int i = 0; i < k; i++) {
for(int j = 0; j <= stocks[i].a; j++) {
if(dp[j] >= 0) {
dp[j] = stocks[i].c;
} else if(j < stocks[i].h || dp[j - stocks[i].h] <= 0) {
dp[j] = -1;
} else {
dp[j] = dp[j - stocks[i].h] - 1;
}
}
}
int ans;
for(int i = 0; i <= stocks[k - 1].a; i++)
if(dp[i] >= 0) ans = i;
printf("%d\n", ans);
}
return 0;
}
poj2392(多重背包dp,可行性类型O(nm)复杂度的解法。)
最新推荐文章于 2019-04-20 17:22:58 发布