2249: Altruistic Amphibians 01背包

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

时间: 2024-10-11 05:40:11

2249: Altruistic Amphibians 01背包的相关文章

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

17-又见01背包

/*                                        又见01背包时间限制:1000 ms  |  内存限制:65535 KB难度:3 描述        有n个重量和价值分别为wi 和 vi 的 物品,从这些物品中选择总重量不超过 W     的物品,求所有挑选方案中物品价值总和的最大值.    1 <= n <=100    1 <= wi <= 10^7    1 <= vi <= 100    1 <= W <= 10^

HDU - 2602 Bone Collector(01背包讲解)

题意:01背包:有N件物品和一个容量为V的背包.每种物品均只有一件.第i件物品的费用是volume[i],价值是value[i],求解将哪些物品装入背包可使价值总和最大. 分析: 1.构造二维数组:dp[i][j]---前i件物品放入一个容量为j的背包可以获得的最大价值. dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - volume[i]] + value[i]);---(a) (1)dp[i - 1][j]---不放第i件物品,因此前i件物品放入一个容量为

01背包

这里就只放自己刷的题目了,毕竟是弱弱哒 HDU2546:饭卡 1 #include <algorithm> 2 #include <cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int n,m; 9 while (~scanf("%d", &n), n) 10 { 11 int f[2013] = {0}, menu[2013] = {0}; 12 for (int i = 1; i <

hdu 1864 01背包 最大报销额

http://acm.hdu.edu.cn/showproblem.php?pid=1864 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的选拔 最大报销额 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18562    Accepted Submission(s): 5459

超大背包(挑战编程之01背包)

先来温习01背包: 01背包是在M件物品取出若干件放在空间为W的背包里,每件物品的体积为W1,W2--Wn,与之相对应的价值为P1,P2--Pn. 求出获得最大价值的方案. 注意:在本题中,所有的体积值均为整数. 思路: 考虑用动态规划的方法来解决,这里的:阶段是:在前N件物品中,选取若干件物品放入背包中:状态是:在前N件物品中,选取若干件物品放入所剩空间为W的背包中的所能获得的最大价值:决策是:第N件物品放或者不放:由此可以写出动态转移方程:我们用f[i,j]表示在前 i 件物品中选择若干件放

uva10201 - Adventures in Moving - Part IV(01背包)

题目:uva10201 - Adventures in Moving - Part IV(01背包) 题目大意:一辆车要走D距离,然后它有个200L油箱,并且一开始有100L,现在给你一路上你会遇到的加油站,和这个加油站每升油的价钱,要求你最后到终点的时候油需要大于等于100L,问你加油最少的费用.如果到达不了目标地点就输出Impossible. 解题思路:首先要先到达这个加油站,然后就相当这个加油站你选不选择加油,选择加油了那么又要加多少油.dp[j][i]代表到达第i个加油站还有jL油,dp

HDU 2955 Robberies --01背包变形

这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即转化为01背包问题. 此时dp[v]表示抢劫到v块钱成功逃跑的概率,概率相乘. 最后从大到小枚举v,找出概率大于逃跑概率的最大v值,即为最大抢劫的金额. 代码: #include <iostream> #include <cstdio> #include <cstring>

RQNOJ 329 刘翔!加油!:01背包

题目链接:https://www.rqnoj.cn/problem/329 题意: 刘翔有n封信,每封信都有自己的欣赏价值value[i].消耗时间time[i].消耗体力h[i].和得到的鼓舞w[i]. 观看信件必须按照价值递增(大于)的顺序观看,不一定需要全看. 可是,刘翔在伤病中,时间和体力分别为t,m,同时看完之后体力不能为0. 问你受到的鼓舞最大为多少. 题解: 这道题里value[i]真的没有用... 表示状态: dp[i][j][k] = max encouraging i:考虑到