-
Notifications
You must be signed in to change notification settings - Fork 0
/
p1065___LIS___Dilworth.java
102 lines (87 loc) · 1.93 KB
/
p1065___LIS___Dilworth.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
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
96
97
98
99
100
101
102
import java.io.*;
import java.util.*;
public class p1065___LIS___Dilworth {
static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
static StreamTokenizer in = new StreamTokenizer(br);
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int n, arr[];
static node node[];
static class node {
int h, w;
node(int h, int w) {
this.h = h;
this.w = w;
}
}
static Comparator<node> cmp = new Comparator<node>() {
public int compare(node o1, node o2) {
if (o1.h < o2.h)
return -1;
else if (o1.h > o2.h)
return 1;
else if (o1.w < o2.w)
return -1;
else if (o1.w > o2.w)
return 1;
return 0;
}
};
public static void main(String[] args) throws IOException {
int test = nextInt();
while (test-- > 0) {
n = nextInt();
node = new node[n + 1];
node[0] = new node(0, 0);
for (int i = 1; i <= n; i++)
node[i] = new node(nextInt(), nextInt());
Arrays.sort(node, cmp);
arr = new int[n + 1];
for (int i = n; i >= 1; i--)
arr[n - i + 1] = node[i].w;
int index, d[] = new int[n + 1];
int l, r, mid;
index = 0;
d[0] = 0;
for (int i = 1; i <= n; i++) {
if (d[index] < arr[i]) {
d[++index] = arr[i];
} else {
l = 0;
r = index;
while (l <= r) {
mid = (l + r) >> 1;
if (d[mid] < arr[i])
l = mid + 1;
else
r = mid - 1;
}
d[l] = arr[i];
}
}
out.println(index);
}
out.flush();
out.close();
}
static String next() throws IOException {
in.nextToken();
return in.sval;
}
static char nextChar() throws IOException {
in.nextToken();
return in.sval.charAt(0);
}
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
static long nextLong() throws IOException {
in.nextToken();
return (long) in.nval;
}
static double nextDouble() throws IOException {
in.nextToken();
return in.nval;
}
}