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 + ;
struct node
{
int l, w, h;
}exa[mx]; bool cmp(node a,node b)
{
return a.w > b.w;
}
const int maxn = 1e8 + ;
int dp[maxn]; int main()
{
int n, d;
cin >> n >> d;
for(int i=;i<n;i++)
{
cin >> exa[i].l >> exa[i].w >> exa[i].h;
}
int ans = ;
sort(exa , exa + n, cmp);
for(int i=;i<n;i++)
{
int w = exa[i].w;
if (exa[i].l + dp[w] > d) ans++;
for(int j=;j<min(w,maxn-w);j++)
{
dp[j] = max(dp[j], dp[j + w] + exa[i].h);
}
}
cout << ans << endl;
return ;
}
2249: Altruistic Amphibians 01背包的更多相关文章
- 2249: Altruistic Amphibians 01背包的应用 + lh的简单图论 图转树求lca
第一个 写了两个比较简单的数论题目,就是整除理论的两个题目,第一个题目比较蠢,第二个稍微要动一点脑筋 Codeforces Round #347 (Div. 2) – A. Complicated G ...
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- 51nod1085(01背包)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...
- *HDU3339 最短路+01背包
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...
- POJ 3624 Charm Bracelet(01背包)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34532 Accepted: 15301 ...
- (01背包变形) Cow Exhibition (poj 2184)
http://poj.org/problem?id=2184 Description "Fat and docile, big and dumb, they look so stupid ...
随机推荐
- Property [*****] not found on type [com.erp.pojo.ErpSupplier]
我实体类里用的是 springboot 里@Slf4j @Data 注解式写的 这样可以减少代码量 ,但是遇到一个问题影响我好长时间 出现了这个错误 Property [*****] not ...
- AJ学IOS(49)多线程网络之线程的创建NSThreand
AJ分享,必须精品 一:NSThread的基本使用 1:创建和启动线程 一个NSThread对象就代表一条线程 创建.启动线程 NSThread *thread = [[NSThread alloc] ...
- LCA基础 附例题(落谷)
https://www.luogu.org/problemnew/solution/P3379 LCA叫做最短公共祖先,用来求距离树上两个节点最近的公共点: 常用倍增算法: #include<i ...
- Hugo博客搭建
HUGO + Github + Github Action持续集成部署个人博客 HUGO本地环境 首先在HUGO的官网下载Hugo的Windows安装包,然后将路径添加到环境变量即可. step1:下 ...
- Yii2.0 rules常用验证规则
设置一个修改方法,但是save(),没有成功,数据修改失败,查了好久,一般情况就是不符合rules规则,而我没有设置rules规则,重新设置了一个不能为空,然后就修改成功,rules里面什么也不写,也 ...
- C++头文件问题
自己定义的头文件必须要用“***.h”系统头文件必须要用<***.h>stdafx.h 必须放在所有头文件的最前面(如果不放,debug版本没有问题:release版本有问题,会报错)
- position的用法(top, bottom, left, right 四个定位属性配合进行使用)
一般情况下 页面元素的定位方式是根据文档流也就是说默认的从上到下,从左到右的方式进行排列的,而将元素从文档流脱离出来显示的方式有两种,一种是 position 定位另一种是float 浮动,这里我们详 ...
- Websec level 30
前言 昨天在易霖博搞的网络安全与执法竞赛看到的一道web题,实际上就是用两个原题凑起来的.. 不过后面的一关没见过这里简单记录一下 第一关 打开是个登录界面,和BJDCTF的简单注入一模一样,连密码都 ...
- linux下批量删除文件
1. 在linux批量删除多级目录下同一格式的文件,可采用find + exec命令组合: 如在删除old目录下的,所有子目录中,后缀为.l的文件方法为: find old -type f -name ...
- Cent OS 7 搭建MySQL
搭建数据库服务器 版本众多,但为了追求稳定选择的是5.7 在使用YUM REPOSITORY官方给出的版本如下: The MySQL Yum repository includes the lates ...