P1250 "trees" of the problem solution

 

Title Description
One side of the street there are several houses, because of environmental reasons residents want to plant some trees on the roadside, residents of the street is divided into n blocks, and No. 1 ... n. Each block has a unit size and a maximum of a tree species. Each resident would like to plant some trees in front of and designated three numbers b, e, t. These three numbers represent the residents want t tree species happened between b and e, of course b≤e, t≤e-b + 1, allowing the sub-area residents may want to cross trees. For reasons of shortage of funds, the environmental protection department you find time to meet the minimum number of trees needed for the requirements of all kinds of trees inhabitants. 

 

Entry
First row n, the number of regions.
The second line h, represents the number of the house.
The following lines describe needs of the residents h: b, e, t (0 <b≤e≤30000, t≤e-b + 1) , respectively, separated by a space.

 

Export
Output is only a number, in order to meet recommended that all residents, the minimum number of trees needs. 

 

Sample input
9
4
1 4 2
4 6 2
8 9 2
3 5 2

 

Sample Output
5

 

prompt
30% of the data satisfies 0 <n≤1000,0 <h≤500;
100% data satisfies 0 <n≤3 × 10 ^ 4, h≤5000,0 <b≤e≤3 × 10 ^ 4, t≤ e-b + 1.

 

answer:

This question being interpreted as greed, applied to the structure. First end position sorting, processing time for each interval. Then front to back scanning interval, the statistics of the number of existing trees, If the selected point more than the number required, then continue, otherwise, from the back, add the missing coverage point. Finally, the output ans.

Greedy reference code:

View Code


This konjac violence with a simple solution to this question, easy to understand

Violence Reference Code:

View Code

 

Sample Download

Guess you like

Origin www.cnblogs.com/ssf-lrk/p/11165966.html