# (Abstract backpack) Luo Gu P1156 trash trap (increase + / provincial election -)

Title Description

Carmen - Farmer John cherished from a Holsteinscow - has fallen into the "junk well" in. "Well garbage" is the local farmers garbage disposal, its depth D (2 \ Le D \ Le 100) D ( 2 D 1 0 0 ) feet.

Carmen wants to garbage up, wait until piled high with the same well, she will be able to flee from the well. In addition, Carmen can sustain their lives by eating junk.

Each can be used to refuse to eat or stacked and piled up garbage without spending time Carmen.

Suppose Carmen know in advance each time garbage dropped T (0 <T \ Le 1000) T ( 0 < T . 1 0 0 0 ), and the height of each Dumping of H (. 1 \ Le H \ Le 25 H ( 1 H 2 5) and eat into the garbage can sustain life time f (1 \ Le f \ Le 30) f ( 1 f 3 0 ), requires the Carmen earliest time to escape out of the well, Suppose the current body has sufficient duration Carmen 10 . 1 0 hours of energy, if Carmen 10 . 1 not eaten within 0 hours, Carmen will starve.

Input Format

The first line 2 two integers, D D, and  G (. 1 \ Le G \ Le 100) G ( . 1 G . 1 0 0 ), G G is the amount of waste being put in the well.

Second to G. 1 + G + . 1 lines each comprising . 3 three integers: T (0 <T <= 1000) T ( 0 < T < = . 1 0 0 0 ), represents a time waste is thrown into the well; F. (. 1 \ Le F. \ Le 30) F. ( . 1 F. . 3 0 ), it indicates that the garbage can maintain time Carmen life; and  H (. 1 \ Le H \ Le 25) H ( . 1 H 2 . 5 ) the garbage can elevate height.

Output Format

If Carmen can climb out of the trap, the output of a whole indicate when the earliest climb out; otherwise the output Carmen can survive the longest long.

Sample input and output

Input # 1
20 4
5 4 9
9 3 2
12 6 10
13 1 1
Output # 1
13

Description / Tips

[Sample Description]

Carmen stacked she received the first garbage: height. 9 = H E I G H T = . 9;

Carmen eat she received the first 2 2 garbage, her life from 10 . 1 extend 0 hours to 13 is . 1 3 hours;

Carmen of stacking . 3 three garbage, height =. 19 H E I G H T = . 1 . 9;

Carmen of stacking . 4 4 garbage, height = 20 is H E I G H T = 2 0.

 

This problem is very like 01 0 1 knapsack, but there are differences, because 01 0 1 knapsack in the "do not install" is not for the state to make changes, do not install sucked garbage up here.

With DP [I] [j] D P [ I ] [ j ] stored in the thrown to I I refuse, height j the maximum value of j life. When DP [I] [J] = 0 D P [ I ] [ J ] = 0, the cowactuallyMoribund state, meaning that, when the value of life is exactly 0 0, of the garbage can cow(This question was stuck here all day ...). Therefore, when the initialization DP D P array to be assigned a negative value [ Tip: Memset t I P : m E m S E positive number t is not really a function assigned to that number, but a large number of values, here 1 and 0 have no effect]

To initialize remember: When thrown to 0 0 garbage, height 0 0, the value of life cows 10 1 0

Similarly, to sort the garbage array, I use the overloaded operator (note that while there may be thrown into the garbage, but there is no special sentence(Probably a water data)You can add a weight value, i.e., when the AT BT == A . T = = B . T, the comparison continues their H H, i.e., the height can be raised to eat)

Another: each piece of the energy consumed by the time R & lt [I +. 1] .tr [I] .T R & lt [ I + . 1 ] . T - R & lt [ I ] . T


Code explanation: backpack cycle, if DP [I] [J] <0 D P [ I ] [ J ] < 0, the state illustrated in this dead cows existed or not, skip. If DP [I] [J] == 0 D P [ I ] [ J ] = = 0, this state is described by a state transition from the other cows moribund, may also operate only once reply less energy than the length of the next period of time, and can not jump out of step, the current state can not be transferred out, the cow died. If the state transition DP D P value is also greater than 0 0, normal (because the garbage is now ordered arrays of)  Tip2: T I P 2 : it must be a transition condition life \ GE length (here, the period of time is ≥ it pits a little)

If you can not jump out of the loop in a backpack, i.e. does not trigger return 0; R & lt E T U R & lt n- 0 ; Description cow jump out, then re-simulation SUM S U m stored energy has been used; if m m (energy) enough to jump (also herein Laid judgment), the rest of the cows (Sadness well) was added to SUM S U m output together.

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int max(int x,int y){return x>y?x:y;}
int dp[101][1001];//dp[i][j]表示在扔进去第i个辣鸡,在j的高度时的最大生命
struct trash
{
int t,f,h;
friend bool operator <(trash a,trash b)
{
return a.t<b.t;
}
}r[101];
int main()
{
memset(dp,-1,sizeof(dp));
int d,g;
scanf("%d%d",&d,&g);
for(int i=1;i<=g;i++)
scanf("%d%d%d",&r[i].t,&r[i].f,&r[i].h);
sort(r+1,r+g+1);
dp[0][0]=10;
r[0].f=0;
r[0].h=0;
r[0].t=0;
for(int i=0;i<g;i++)
for(int j=0;j<=d;j++)
{
IF (DP [I] [J] <state 0) // No cows
Continue;
IF (J + R & lt [I +. 1] .h> = D && DP [I] [J]> = R & lt [I +. 1] when .tr [i] .t) // transfer this state, next only to meet a garbage can directly jump out, but also to meet the cow's energy to hold out until the next garbage.
{
The printf ( "% D \ n-", R & lt [I +. 1] .T);
return 0;
}
IF (DP [I] [J] -R & lt [I +. 1] R & lt .T + [I] .T> 0 =)
DP [I +. 1] [R & lt J + [I +. 1] .h] DP = [I] [J] -R & lt [I +. 1] R & lt .T + [I] .T;
IF (DP [ I] [J] -R & lt [I +. 1] R & lt .T + [I] .T> = 0)
DP [. 1 + I] [J] = max (DP [. 1 + I] [J], DP [I ] [j] -r [i + 1] .t + r [i] .t + r [i + 1] .f); // max herein as dp [i + 1] [j ] value would have been possible
}
// If the current state, indicating that did not jump out
// full eat again re-simulation
@ cow when not adhere, to now run out of energy
int m = 10, sum = 0 ; // m is now the energy, sum the energy has already been used
for (int I =. 1; I <= G; I ++)
{
IF (R & lt [I] .tr [-I. 1] .T> m)
{
the printf ( "% D \ n-", SUM + m);
return 0;

sum+=r[i].t-r[i-1].t;
m-=r[i].t-r[i-1].t;
m+=r[i].f;
}
printf("%d\n",sum+m);
return 0;
}

 

Guess you like

Origin www.cnblogs.com/little-cute-hjr/p/11422055.html