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 5output
Copy
3input
Copy
3 19 14 5 3 12 4 4 20 10 5output
Copy
2題意:
? ? 有n個(gè)青蛙被困在了一口深度為d的井里,對于每個(gè)青蛙有三種參數(shù)(l,w,h)分別代表它的最大跳躍的高度,它的體重以及它的身高。現(xiàn)在他們打算采用疊羅漢的方式讓盡可能多的青蛙逃離這口井,但在疊羅漢的過程中,上面的青蛙的重量要嚴(yán)格小于下面的青蛙的重量。現(xiàn)在問你最多能夠有多少只青蛙能夠成功逃生。
題目分析:
??? 一個(gè)挺有意思的題目。首先考慮這樣的問題:倘若要讓盡可能多的青蛙能夠逃跑,則顯然羅漢最好疊得盡可能的高(這才能使得那些不能一次性跳出的青蛙能夠逃離)。
??? 而顯然,對于那些體重最大的青蛙,他們顯然不能疊在其他青蛙上,因此我們首先對青蛙的重量從大到小進(jìn)行排序,其次我們考慮第i個(gè)青蛙的重量對于其他重量小的重量的青蛙的狀態(tài)的轉(zhuǎn)移。
??? 我們設(shè)為重量為i的青蛙最高能夠跳的高度,而對于第i個(gè)重量為的青蛙,不難想到最多一定會有個(gè)重量小于的青蛙能夠跳到它的上面,故可得有狀態(tài)轉(zhuǎn)移方程()
代碼:
#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\n",res); }?
轉(zhuǎn)載于:https://www.cnblogs.com/Chen-Jr/p/11007170.html
總結(jié)
以上是生活随笔為你收集整理的Gym 101933 A(dp)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker入坑指南之RUN
- 下一篇: 10th blog:Object