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;}