Description
A set of frogs have accidentally fallen to the bottom of a large pit. Their only means of escaping the pit is to jump out of it. Each frog i? is described by three parameters (li,?wi,?hi)? where li? is its leap capacity, wi? its weight, and hi? its height. The leap capacity specifies how high that frog can jump. If a frog‘s leap capacity is strictly larger than the depth of the pit, the frog can directly escape the pit. However, these frogs are altruistic. Rather than selfishly saving themselves and leaving the frogs with too limited leap capacity behind, they collectively aim to save as many of them from the pit as possible.
The frogs realize that if a frog A? climbs up on the back of frog B? before it jumps, the first frog A? stands a better chance of escaping the pit: it can escape if hB?+?lA? is strictly larger than the depth of the pit.
Furthermore, if frog B carrying frog A on its back climbs up on the back of frog C, the situation is even better for frog A: it can now escape the pit if hC?+?hB?+?lA is strictly larger than the depth of the pit.
The frogs can build even higher piles of frogs this way, the only restriction is that no frog may carry other frogs of weight in total amounting to its own weight or heavier. Once a pile has been used to allow a frog to escape, the frogs in the pile jump back to the bottom of the pit and they can then form a new pile (possibly consisting of a different set of frogs). The question is simply how many frogs can escape the pit assuming they collaborate to maximize this number?
Input
The first line of input contains two integers n? and d? (1?≤?n?≤?100?000?, 1?≤?d?≤?108?), where n? is the number of frogs and d? is the depth of the pit in µm. Then follow n? lines each containing three integers l,?w,?h? (1?≤?l,?w,?h?≤?108?), representing a frog with leap capacity l? µm, weight w? µg, and height h? µm. The sum of all frogs‘ weights is at most 108? µg.
Output
Output the maximum number of frogs that can escape the pit.
Sample Input
3 19 15 5 3 12 4 4 20 10 5
Sample Output
3
Hint
Source
ncpc2018
这个就是一个01背包,好难看出来啊,开始我以为是贪心,昨天的一个状压dp我也以为是贪心,
所以呢,如果我觉得像贪心但是又不能肯定的,那应该是dp
代码要是看不懂,那就模拟一次就会了
#include <cstring> #include <cstdlib> #include <cstdio> #include <string> #include <cmath> #include <algorithm> #include <queue> #include <iostream> #define inf 0x3f3f3f3f using namespace std; typedef long long ll; const int mx = 1e5 + 10; struct node { int l, w, h; }exa[mx]; bool cmp(node a,node b) { return a.w > b.w; } const int maxn = 1e8 + 10; int dp[maxn]; int main() { int n, d; cin >> n >> d; for(int i=0;i<n;i++) { cin >> exa[i].l >> exa[i].w >> exa[i].h; } int ans = 0; sort(exa , exa + n, cmp); for(int i=0;i<n;i++) { int w = exa[i].w; if (exa[i].l + dp[w] > d) ans++; for(int j=1;j<min(w,maxn-w);j++) { dp[j] = max(dp[j], dp[j + w] + exa[i].h); } } cout << ans << endl; return 0; }
原文地址:https://www.cnblogs.com/EchoZQN/p/11478920.html