Poj1089: intervals

Source: Internet
Author: User
1089: intervals 
Total time limit: 1000 ms memory limit: 65536kb description There is given the series of N closed intervals [ai; B I], where I = ,..., n. the sum of those intervals may berepresented as a sum of closed pairwise non −intersecting intervals. the task is to find such representation with the minimal number of intervals. the intervals of this representation shocould be written in the output file in acceding order. we say that the intervals [A; B] and [C; D] are in ascending order if, and only if a <= B <C <= D. taskwrite a program which :. reads from the STD input the description of the series of intervals ,. computes pairwise non − intersecting intervals satisfying the conditions given abve ,. writes the computed intervals in ascending order into STD output input in the first line of input there is one integer N, 3 <= n <= 50000. this is the number of intervals. in the (I + 1) −st line, 1 <= I <= N, there is a description of the interval [ai; BI] in the form of two integers AI and Bi separated by a single space, which are respectively the beginning and the end of the interval, 1 <= AI <= Bi <= 1000000. output the output shoshould contain descriptions of all computed pairwise non-Intersecting intervals. in each line shoshould be written a description of one interval. it shoshould be composed of two integers, separated by a single space, the beginning and the end of the interval respectively. the intervals shoshould be written into the output in ascending order. sample input 55 61 410 106 98 10 sample output 1 45 10

I was speechless when I made this question ....

In the past few days, there have been too many questions about the line segment tree. As a result, when we see what the line segment is, we will first think of the Line Segment tree to solve it... After writing the result for an hour, I felt that something was wrong. The data size of the tree on 1000000 leaf nodes... The time spent on building an estimate is time-out... The first method was to build a tree, and then use consult to query whether it was overwritten and sort it by the Left endpoint, use binary to find out the maximum right endpoint that is just covered to meet the conditions...

Quick Start crying... I really don't have the confidence to write it down. Baidu just wrote some of your heroes to find myself stupid...

Final Solution:

Simply arrange the order from small to large based on the left endpoint, and traverse the past. Then, it is OK, and combine the overlapping parts until there is no crossover interval, then output the previous interval, and finally output the last interval.

Code:

 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4  5 using namespace std; 6  7 struct P 8 { 9     int l;10     int r;11 }p[50001];12 13 bool cmp(const P& a,const P& b)14 {15     return a.l < b.l;16 }17 18 int main()19 {20     int n;21     scanf("%d",&n);22     for (int i = 0; i < n; ++i)23     {24         scanf("%d%d",&p[i].l,&p[i].r);25     }26     sort(p,p+n,cmp);27     int x = p[0].l,y = p[0].r;28     for(int i = 0; i < n; ++i)29     {30         if(p[i].l >= x && p[i].l <=y)31         {32             if(p[i].r > y)33                 y = p[i].r;34         }35         else36         {37             printf("%d %d\n",x,y);38             x = p[i].l;39             y = p[i].r;40         }41     }42     printf("%d %d\n",x,y);43     return 0;44 }
View code

 

 

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.