Interval coincidence judgment [poj2808 the tree outside the gate]

Source: Internet
Author: User

Title: http://bailian.openjudge.cn/practice/2808/

Reference the article, rewrite the code: http://www.cnblogs.com/youxin/p/3266617.html (Note: The original Solution 2 code is wrong)

Solution 1: space-changing time

#include <iostream>using namespacestd;intMain () {intL, M, I, start, end, count; BOOLtrees[10001];  for(i =0; I <10001; i++) Trees[i]=true; Count=0; CIN>> L >>M;  for(i =0; i < M; i++) {cin>> Start >>end;  while(Start <=end) {Trees[start]=false; Start++; }    }     for(i =0; I <= L; i++)        if(Trees[i] = =true) Count++; cout<<count; return 0;}

How the length of the road, such as 4 billion, so large as to open the array space, this method of failure. At this point, you can use Solution 2: First the interval according to the start time starting ascending order, and then when the other interval start is less than the initial interval (denoted by the temp) end, the two intervals are coincident, merging (and modifying Temp.end). Since start is sorted by interval start, as long as start is not less than the initial interval end, then the interval below does not overlap with the initial interval, at which time the temp interval length is added to count, and the temp is changed to a new zone without overlap in the front interval, and continues to scan downward.

Solution 2: Interval Merging

#include <stdio.h>#include<stdlib.h>#defineM_max 100+1typedefstructarea{intstart; intend;} area;intComparearea (Const void*a,Const void*b) {    return(area *) a)->start-((area *) b)start;}intMain () {area Area[m_max], temp; intL, M, Count =0; scanf ("%d%d", &l, &M);  for(inti =0; i < M; i++) {scanf ("%d%d", &area[i].start, &area[i].end); } qsort (area, M,sizeof(area), Comparearea);//interval in ascending order of start timeTemp= area[0];  for(inti =1; i < M; i++){        if(Area[i].start <=temp.end) {            if(Area[i].end >temp.end) Temp.end=Area[i].end; }        Else{Count+ = Temp.end-temp.start +1; Temp=Area[i]; }} Count+ = Temp.end-temp.start +1; printf ("%d", L +1-count); return 0;}

The beauty of programming has similar problems, in fact, the nature of the tree problem is the interval coincidence judgment.

One, the question:

1. Given a source interval [x, y] and n unordered target range [x1,y1] [x2,y2] ... [Xn,yn], judging the source range [x, Y] is not within the target range.

2. Given a window area and n windows on the system interface, determine whether the window area is covered by an existing window.

First question source code:

Follow the instructions given in the beauty of programming. The target interval is sorted by the left boundary value of the interval O (NLOGN), the ordered interval is combined O (n), the source interval to be searched, and two points are used to find out in which source interval in which the left and right boundary points are merged (LOGN), If it belongs to the same source range, it is in the target range, otherwise it is not. (Why the dichotomy is possible)

Interval coincidence judgment [poj2808 the tree outside the gate]

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.