A.Altruistic Amphibians---Gym101933(01背包的运用)

Altruistic Amphibians

Time Limit: 6 Sec Memory Limit: 1024 Mb

练习链接https://codeforces.com/gym/101933/problem/A

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 ( l i ,   w i ,   h i li, wi, hi 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 h B   +   l A hB + lA 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 ≤ 10^8​), 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 ≤ 10 ^8​), representing a frog with leap capacity l​ µm, weight w​ µg, and height h​ µm. The sum of all frogs’ weights is at most 10 ^8​ µ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


这道题的大意是这样的,给你青蛙的数量和井的深度,接下来n个青蛙的跳跃极限,体重和身高,它们可以像叠罗汉一样叠起来,但底下的青蛙体重必须大于上面的之和,问,最多可以有几个青蛙跳出井。

emmm,老实说当时的测评出了些问题,导致交的人全都RE(本来是MLE的,测评机的锅)了,后来空间限制开放了些,重测就过了。

这题因为是重的在下面,所以可以先排一下序,排完序之后保证了后面的数据小于前面的数据,因此可以转化为01背包问题了,即该青蛙是叠上去还是不叠上去,当由于井的高度有点恐怖(1亿),所以当数组开到1亿时,第一遍果断RE(测评机故障导致MLE显示RE)当时我就觉得应该是数组太大,但看到是RE觉得有点奇怪,我觉得应该不是01背包的算法(需要的空间太大了)。后来出公告了:空间限制开放,重判。emmm。。

在这里插入图片描述
好像刚开始的空间限制是100MB+来着。

#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5+5, M = 1e8+10;
struct fro {
    int l, w, h;
}a[N];
int dp[M];
bool cmp(fro a,fro b){
    return a.w>b.w;
}
int main()
{
    int n, gao, ans = 0;
    scanf("%d%d", &n, &gao);
    for(int i = 0; i < n; i++)
        scanf("%d%d%d", &a[i].l, &a[i].w, &a[i].h);
    sort(a, a+n,cmp);
    for(int i = 0; i < n; i++) {
        int w = a[i].w;
        if( dp[w]+a[i].l > gao) ans++;   
        for(int x = 1; x < min(w,M-w); x++) {
            dp[x] = max(dp[x], dp[x+w]+a[i].h);    //体重1-x能叠的最大高度
        }
    }
    printf("%d\n", ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值