1290 non-overlapping Line Segments

Source: Internet
Author: User
Description

Some people say that scientific research is not very advanced. It is nothing more than discovering and solving problems. Wesley believes that anyone who is good at thinking can become a scientist. However, he is a little depressed recently. He thinks that in the classroom, knowledge is often obtained only by "Indoctrination", rather than thinking. In his spare time, he began to think about some problems. The descriptions of these problems were very simple. For example, the following line segment does not overlap.

There are N line segments on the number axis. How many lines must be removed to make the remaining line segments not overlap?

Input

Multiple Input groups. The first positive integer t represents the number of groups.

The first row of each group has a positive integer N, 1 ≤ n ≤ 100000, indicating the number of line segments.

Next, row N has two integers, namely, S. F (0 <S <F, and within the range indicated by int32) represents the start and end points of a line segment.

Output

The output must at least remove the number of line segments.

Sample Input

131 51 33 4

Sample output

1

 

 

#include <iostream>#include <algorithm>#include "stdio.h"using namespace std;typedef struct node{int a, b;};bool cmp(node a, node b){if(a.a != b.a){return a.a < b.a;}else{return a.b < b.b;}}node data[100002];int main(){int t;scanf("%d", &t);while(t --){int n, i;scanf("%d", &n);for(i = 0; i < n; i ++){scanf("%d%d", &data[i].a, &data[i].b);}sort(data, data + n, cmp);int k = 0, num = 0;for(i = 1; i < n; i ++){if(data[i].a <= data[i-1].a){num ++;}else{if(data[i].a < data[k].b){num ++;if(data[i].b <= data[k].b){k = i;}}else{k = i;}}}printf("%d\n", num);}return 0;}

 

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.