"Shanghai Jiaotong University OJ" Water gun extinguishing (calculate interval length)

Source: Internet
Author: User

1025. Water gun Fire Description

To commemorate the fire victims of "11 15" high-rise buildings in Shanghai

On the afternoon of November 15, a serious fire occurred in a 28-storey residential building at No. 728 Jiaozhou Road, Jingan, Shanghai. Fire department after the police immediately dispatched 25 fire Squadron, hundreds fire truck put into the fire rescue operation, emergency evacuation and rescue hundreds of people near the residents. 58 people were killed as a result of the fire.

At this time all floors of the building have been surrounded by fire, each fire engine's high-pressure water gun can be on a number of successive floors of the fire, the tall buildings as the vertical y-axis (the origin can be placed in any position), if a fire engine to [10,1000] This area of fire, then 10 to 1000 of this area of the fire was put Request out the total length of the area that has been extinguished after all fire engines have finished extinguishing work.

The high-pressure water cannon plays a key role in controlling the fire. Unfortunately, for the 28 floor of the building, Shanghai high-pressure water gun spray height "only to 10 floor."

Input Format

First line: N (number of fire engines)

Next n rows, two numbers per line: Ai Bi (indicates the starting and ending positions of the fire extinguishing area of the fire engine)

−< Span id= "mathjax-span-5" class= "mn" >10 9 ≤ai ,b I≤ 109

N≤20000

If ai=BI, this area is a point, as no length.

Output Format

The total length of the output area to be extinguished.

Sample Input
3-1 15 112 9
Sample Output
11
=======================================================================
The implementation method is more, the overall is the first sort
1. Only the left boundary of the interval is sorted, and when the length is calculated, it is possible to scan more points for a point (larger right boundary).
2. The left and right boundary are sorted, then each corresponding to the formation of the interval, to a range only to judge and whether the next interval can be repeated;
3. This idea is more ingenious, the left and right borders are sorted at the same time, and then traversed to count the remaining left boundary number, while recording the current minimum left boundary, when the maximum left boundary is encountered (that is, the remainder is 0 o'clock) to calculate the interval length, the next point is recorded as the new left boundary.
I use the third, in the implementation of a small problem caused by a point does not pass, that is, when the sort, if there are several same number, then it is possible that the right edge of the number of rows on the left, so that the number of times the left boundary is greater than the right edge, so in order to consider the same time the left side of the Here's the code:
1#include <iostream>2#include <algorithm>3 using namespacestd;4 5 structonepoint{6     Long LongCoord//Interval Boundary value7     BOOLDire//left or right boundary8 };9 BOOLCompare (OnePoint a,onepoint b) {Ten     if(A.coord==b.coord && A.dire < B.dire)return 1;//Make sure left border is left One     returna.coord<B.coord; A } - intMain () { -     intn,i,j; the      -Cin>>N; -OnePoint interval[2*N]; -      for(i=0; i<n;++i) { +cin>>interval[2*i].coord>>interval[2*i+1].coord; -interval[2*i].dire =0; +interval[2*i+1].dire =1; A     } atSort (interval,interval+2*n,compare); -     Long LongLength =0; -     Long Longleft = interval[0].coord; -     Long LongNum_left =1; -      for(i=1;i<2*n;++i) { -         if(num_left==0) left = Interval[i].coord;//update leftmost boundary value in         if(Interval[i].dire) num_left--; -         Elsenum_left++;//Update left boundary number to         if(num_left==0) Length+=interval[i].coord-left;//Update Length +     } -cout<<length; the     return 0; *}
View Code

"Shanghai Jiaotong University OJ" Water gun extinguishing (calculate interval length)

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.