Http://acm.nyist.net/JudgeOnline/problem.php? Pid = 1, 891
Time limit: 2000 MS | memory limit: 65535 KB Difficulty: 2
-
Description
-
In mathematics class, the teacher gave LYH some closed intervals so that he could set as few points as possible, so that each closed interval had at least one point. But LYH is too busy these days. Can you help him?
-
Input
-
Multiple groups of test data.
Enter N for each group of data, indicating that there are N closed intervals (N ≤ 100 ).
In the next N rows, enter two numbers a and B in each row (0 ≤ a ≤ B ≤ 100), indicating the two endpoints of the interval.
-
Output
-
The output is an integer that indicates at least a few points.
-
Sample input
-
4 1 5 2 4 1 4 2 3 3 2 3 4 5 6 1 2 2
-
Sample output
-
1 3 1
Solution: greedy, range coverage. Sort all intervals in ascending order of the right endpoint. If the right endpoint is the same, sort them in ascending order of the left endpoint, and take the first right endpoint as the first point, greedy left endpoint, if the left endpoint is after the right endpoint. Update the right endpoint and the number of points is + 1.
1
2 # include <stdio. h>
3 # include <stdlib. h>
4 # include <string>
5
6 struct P {
7 int a, B;
8} p [110];
9
10 int cmp (const void * a, const void * B ){
11 struct P * c = (struct P *);
12 struct P * d = (struct P *) B;
13 if (c-> B! = D-> B ){
14 return c-> B-d-> B;
15}
16 else {
17 return c-> a-d->;
18}
19}
20
21 int main (){
22 int n, I, ans, t;
23 while (scanf ("% d", & n )! = EOF ){
24 for (I = 0; I <n; I ++ ){
25 scanf ("% d", & p [I]. a, & p [I]. B );
26}
27 qsort (p, n, sizeof (p [0]), cmp );
28 ans = 1, I = 0;
29 t = I;
30 for (I = 1; I <n; I ++ ){
31 if (p [I]. a> p [t]. B ){
32 ans ++;
33 t = I;
34}
35}
36 printf ("% d \ n", ans );
37}
38 return 0;
39}
40
NYOJ-891-find a point