import java io BufferedReader import java io IOException import java i

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {
public static void qSort(int[] sort_array, int[] array, int l, int r) {
int i = l;
int j = r;
int x = sort_array[r];
while (i <= j) {
while (sort_array[i] < x) {
i++;
}
while (sort_array[j] > x) {
j--;
}
if (i <= j) {
int temp = sort_array[i];
sort_array[i] = sort_array[j];
sort_array[j] = temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
if (l<j){
qSort(sort_array, array, l, j);
}
if(i<r){
qSort(sort_array, array, i, r);
}
}
public static void main(String[] args) {
int n = 0;
try {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
// while((input=br.readLine())!=null){
//
// }
} catch (IOException e) {
e.printStackTrace();
}
int[] l_array = new int[n];
int[] r_array = new int[n];
try{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < n; i++){
String input;
input = br.readLine();
if (input != null) {
String[] strings = input.split(" ");
l_array[i] = Integer.parseInt(strings[0]);
r_array[i] = Integer.parseInt(strings[1]);
}
}
}catch(IOException io){
io.printStackTrace();
}
boolean[] l_bool = new boolean[n];
for (int i = 0; i < n; i++){
l_bool[i] = false;
}
int cur_index = 0;
int r_prev = 0;
int[] points = new int[n];
qSort(r_array, l_array, 0, n - 1);
for (int i = 0; i < n; i ++){
if (l_array[i] > r_prev) {
points[cur_index] = r_array[i];
cur_index++;
r_prev = r_array[i];
}
}
System.out.println(cur_index);
for (int i = 0; i < cur_index; i++) {
System.out.println(points[i]);
}
}
}