P1156 spam traps and p1156 spam traps

Source: Internet
Author: User

P1156 spam traps and p1156 spam traps
Description

Karman-A Holsteins cow that farmer John cherishes-has fallen into the garbage trap. A Spam is a place where farmers throw garbage. Its depth is D (2 <= D <= 100) feet.

Karman wants to pile up the garbage and wait until the pile is as high as the well, then she will be able to escape from the well. In addition, Carmen can maintain his own life by eating garbage.

Each Spam can be used to eat or pile up, and it does not take Karman's time to pile up the spam.

Assume that Carmen knows in advance the time t (0 <t <= 1000) of each garbage disposal and the height h (1 <= h <= 25) of each garbage stack) and the time (1 <= f <= 30) when the garbage can be kept alive. It is required that Carmen be able to escape from the well at the earliest, assuming that Karman's current body has enough energy to last for 10 hours, if Karman does not eat for 10 hours, Karman will starve to death.

Input/Output Format Input Format:

The first behavior is two integers: D and G (1 <= G <= 100), and G is the number of garbage into the well.

The row from the second row to the G + 1 contains three integers: T (0 <T <= 1000), indicating the time when the garbage was thrown into the well; F (1 <= F <= 30) indicates the time when the garbage can maintain Carmen's life; and H (1 <= H <= 25) indicates the height of the garbage.

Output Format:

If Carmen can climb out of the trap, output a whole to indicate the earliest time when he can climb out; otherwise, output the maximum time for Karman to survive.

Input and Output sample Input example #1:
20 45 4 99 3 212 6 1013 1 1
Output sample #1:
13
Description

[Example]

Carmen stacked the first spam she received: height = 9;

Carmen eats the second spam she received, extending her life from 10 hours to 13 hours;

Carmen piled up 3rd garbage items, height = 19;

Karman piled up 4th pieces of garbage, height = 20.

 

Sort each spam in ascending order of appearance time

Define a. x as the occurrence time a. h as the height a. t as the consumed blood volume

F [I] [j] indicates the maximum remaining blood volume of the first garbage at the height of j.

Obviously, f [0] [0] is 10 at the beginning.

If I is the height of the spam number j, It is shown as follows:

1. eat this garbage f [I] [j] = max {f [I] [j], f [I-1] [j]-(a [I]. x-a [I-1]. x) + a [I]. t}

The initial f [I] [j] is-INF, a [I]. x-a [I-1]. x is the time it takes to get from the previous spam to this spam.

And it is obvious to be f [I-1] [j]-(a [I]. x-a [I-1]. x)> = 0: the garbage can be consumed only when the current time is alive. The amount of blood consumed after the garbage is a [I]. t, and this cow is very good at eating the next spam when it just died.(+ 1 s)

2. fold the garbage f [I] [j] = max {f [I] [j], f [I-1] [j-a [I]. h]-a [I]. x + a [I-1]. x}

In this case, it is clear that if we can place the current garbage and reach the j height, the current height will be subtracted from the current garbage height a [I]. h, that is, ([j-a [I]. h) There must be one that can be passed through (a [I]. x-a [I-1]. x). Of course, j-a [I]. h must be greater than or equal to 0. It is obviously impossible to set the time to a negative number.

In addition, this cow is very powerful and can still pile up a garbage in the last second of his life.(+ 1 s)

If the enumerated height j reaches the target height n (to meet the blood volume> = 0), we can directly output a [I]. x

Why? Because we arranged the order at the beginning, and we enumerated each garbage as an external loop, when a j reaches the target height, it must be the best.

When we have the optimal solution, we can exit directly.

However, if the optimal solution is not found, we will enumerate it again. At this time, the answer ans is max {ans, f [I] [j] + a [I]. x}

Because we can continue to support f [I] [j] s after arriving at I garbage.

In this way, you can

Experiments show that you don't have to judge whether there is a negative height or blood volume.(I wonder if it's data water ?)

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 # define INF 0x7f7f7f7f 7 using namespace std; 8 int deep, n; 9 struct node10 {11 int t; // time 12 int life; // life 13 int h; // cushion height 14} a [1001]; 15 int comp (const node & a, const node & B) 16 {17 return. t <B. t; 18} 19 int dp [1001] [1001]; 20 int main () 21 {22 scanf ("% d", & deep, & n ); 23 for (int I = 1; I <= n; I ++) 24 scanf ("% d", & a [I]. t, & a [I]. life, & a [I]. h); 25 26 for (int I = 0; I <= n; I ++) 27 for (int j = 0; j <= deep; j ++) 28 dp [I] [j] =-INF; 29 30 sort (a + 1, a + n + 1, comp); 31 dp [0] [0] = 10; 32 a [0]. h = a [0]. life = a [0]. t = 0; 33 int flag = 0; 34 35 for (int I = 1; I <= n; I ++) 36 {37 for (int j = 0; j <= deep; j ++) 38 {39 if (dp [I-1] [j]-a [I]. t + a [I-1]. t> = 0) 40 {41 dp [I] [j] = max (dp [I] [j], dp [I-1] [j]-a [I]. t + a [I-1]. t + a [I]. life); 42} 43 if (dp [I-1] [j-a [I]. h]-a [I ]. T + a [I-1]. t> = 0 & j-a [I]. h> = 0) 44 {45 dp [I] [j] = max (dp [I] [j], dp [I-1] [j-a [I]. h]-a [I]. t + a [I-1]. t); 46 if (j = deep) 47 {48 printf ("% d", a [I]. t); 49 flag = 1; 50 return 0; 51} 52} 53} 54} 55 int ans = 0; 56 if (flag = 0) 57 {58 for (int I = 0; I <= n; I ++) 59 {60 for (int j = 0; j <= deep; j ++) 61 {62 if (dp [I] [j]! = INF) 63 ans = max (ans, dp [I] [j] + a [I]. t); 64} 65} 66} 67 68 printf ("% d", ans); 69 return 0; 70}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.