-
Notifications
You must be signed in to change notification settings - Fork 0
/
2370.java
68 lines (62 loc) · 2.14 KB
/
2370.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static StringTokenizer st;
static String[] split;
static String line;
public static void main(String[] args) throws IOException {
int n = nextInt();
int[][] inputCoordinates = new int[n][2];
Set<Integer> originalCoodinates = new TreeSet<>();
Map<Integer, Integer> compressionMap = new HashMap<>();
for (int i = 0; i < n; ++i) {
int a = nextInt();
int b = nextInt();
inputCoordinates[i][0] = a;
inputCoordinates[i][1] = b;
originalCoodinates.add(a);
originalCoodinates.add(b);
}
int compressionValue = 0;
for (int e : originalCoodinates) {
compressionMap.put(e, compressionValue++);
}
int m = originalCoodinates.size();
int[] compressedArray = new int[m];
for (int i = 0; i < inputCoordinates.length; i++) {
int[] coordinate = inputCoordinates[i];
int left = compressionMap.get(coordinate[0]);
int right = compressionMap.get(coordinate[1]);
for (int j = left; j <= right; ++j) {
compressedArray[j] = i + 1;
}
}
boolean[] visited = new boolean[n + 1];
int ans = 0;
for (int i = 0; i < m; ++i) {
int posterNum = compressedArray[i];
if (!visited[posterNum]) {
visited[posterNum] = true;
++ans;
}
}
System.out.println(ans);
}
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
}