zoukankan      html  css  js  c++  java
  • Gym 101933 A(dp)

    传送门:

    题面:

    A. Altruistic Amphibians

    time limit per test

    3.0 s

    memory limit per test

    512 MB

    input

    standard input

    output

    standard output

    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 ii is described by three parameters (li,wi,hi)(li,wi,hi) where lili is its leap capacity, wiwi its weight, and hihi 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 AA climbs up on the back of frog BB before it jumps, the first frog AA stands a better chance of escaping the pit: it can escape if hB+lAhB+lA is strictly larger than the depth of the pit.

    Furthermore, if frog BB carrying frog AA on its back climbs up on the back of frog CC, the situation is even better for frog AA: it can now escape the pit if hC+hB+lAhC+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 nn and dd (1≤n≤1000001≤n≤100000, 1≤d≤1081≤d≤108), where nn is the number of frogs and dd is the depth of the pit in μmμm. Then follow nn lines each containing three integers l,w,hl,w,h (1≤l,w,h≤1081≤l,w,h≤108), representing a frog with leap capacity ll μmμm, weight ww μgμg, and height hh μmμm. The sum of all frogs' weights is at most 108108 μgμg.

    Output

    Output the maximum number of frogs that can escape the pit.

    Examples

    input

    Copy

    3 19
    15 5 3
    12 4 4
    20 10 5
    

    output

    Copy

    3
    

    input

    Copy

    3 19
    14 5 3
    12 4 4
    20 10 5
    

    output

    Copy

    2

    题意:

        有n个青蛙被困在了一口深度为d的井里,对于每个青蛙有三种参数(l,w,h)分别代表它的最大跳跃的高度,它的体重以及它的身高。现在他们打算采用叠罗汉的方式让尽可能多的青蛙逃离这口井,但在叠罗汉的过程中,上面的青蛙的重量要严格小于下面的青蛙的重量。现在问你最多能够有多少只青蛙能够成功逃生。

    题目分析:

        一个挺有意思的题目。首先考虑这样的问题:倘若要让尽可能多的青蛙能够逃跑,则显然罗汉最好叠得尽可能的高(这才能使得那些不能一次性跳出的青蛙能够逃离)。

        而显然,对于那些体重最大的青蛙,他们显然不能叠在其他青蛙上,因此我们首先对青蛙的重量从大到小进行排序,其次我们考虑第i个青蛙的重量对于其他重量小的重量的青蛙的状态的转移。

        我们设dp[i]为重量为i的青蛙最高能够跳的高度,而对于第i个重量为w[i]的青蛙,不难想到最多一定会有w[i]个重量小于w[i]的青蛙能够跳到它的上面,故可得有状态转移方程dp[j-w[i]]=max(dp[j-w[j]],dp[j]+h[i])w[i]<=j<=2*w[i]

    代码:

    #include <bits/stdc++.h>
    #define maxn 100000005
    using namespace std;
    int dp[maxn];
    struct Node{
        int l,w,h;
        bool operator <(const Node &b)const{
            return w>b.w;
        }
    }q[100005];
    int main()
    {
        int n,d;
        scanf("%d%d",&n,&d);
        for(int i=0;i<n;i++) scanf("%d%d%d",&q[i].l,&q[i].w,&q[i].h);
        sort(q,q+n);
        int res=0;
        for(int i=0;i<n;i++){
            if(dp[q[i].w]+q[i].l>d) res++;
            for(int j=q[i].w;j<min(2*q[i].w,(int)1e8+2);j++){
                dp[j-q[i].w]=max(dp[j-q[i].w],dp[j]+q[i].h);
            }
        }
        printf("%d
    ",res);
    }
  • 相关阅读:
    Java接收Cordys中webservice接口的返回数据并解析xml获取相应节点数据
    初识jenkins之-jenkins的安装与配置
    Linux(centos6.8)下Tomcat的安装与配置
    [置顶] Linux下(centos6.8)JDK1.8的安装与配置
    小白五一期间个人建站
    声明一些事
    js中apply()的用法淺談
    angularJS中的服务
    PHP中的常用关键字
    PHP中三大特性---继承性
  • 原文地址:https://www.cnblogs.com/Chen-Jr/p/11007170.html
Copyright © 2011-2022 走看看