π‘ Info
λ΄μ©
λ μ λ΄λ Aμ B μ¬μ΄μ νλ λμ© μ κΉμ€μ μΆκ°νλ€ λ³΄λ μ κΉμ€μ΄ μλ‘ κ΅μ°¨νλ κ²½μ°κ° λ°μνμλ€. ν©μ μ μνμ΄ μμ΄ μ΄λ€ μ€ λͺ κ°μ μ κΉμ€μ μμ μ κΉμ€μ΄ κ΅μ°¨νμ§ μλλ‘ λ§λ€λ €κ³ νλ€.
μλ₯Ό λ€μ΄, <κ·Έλ¦Ό 1>κ³Ό κ°μ΄ μ κΉμ€μ΄ μ°κ²°λμ΄ μλ κ²½μ° Aμ 1λ² μμΉμ Bμ 8λ² μμΉλ₯Ό μλ μ κΉμ€, Aμ 3λ² μμΉμ Bμ 9λ² μμΉλ₯Ό μλ μ κΉμ€, Aμ 4λ² μμΉμ Bμ 1λ² μμΉλ₯Ό μλ μ κΉμ€μ μμ λ©΄ λ¨μμλ λͺ¨λ μ κΉμ€μ΄ μλ‘ κ΅μ°¨νμ§ μκ² λλ€.
μ κΉμ€μ΄ μ λ΄λμ μ°κ²°λλ μμΉλ μ λ΄λ μμμλΆν° μ°¨λ‘λλ‘ λ²νΈκ° 맀겨μ§λ€. μ κΉμ€μ κ°μμ μ κΉμ€λ€μ΄ λ μ λ΄λμ μ°κ²°λλ μμΉμ λ²νΈκ° μ£Όμ΄μ§ λ, λ¨μμλ λͺ¨λ μ κΉμ€μ΄ μλ‘ κ΅μ°¨νμ§ μκ² νκΈ° μν΄ μμ μΌ νλ μ΅μ κ°μμ μ κΉμ€μ ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€.
π₯μ λ ₯ 쑰건
8
1 8
3 9
2 2
4 1
6 4
10 10
9 7
7 6
π€μΆλ ₯ 쑰건
3
1
3
4
μ€μ νμ΄ μκ° : 1μκ° 55λΆ(첫 νμ΄ μκ° ν¬ν¨)
μ λ ₯
κ³μ°
μΆλ ₯
import java.util.*;
class Line {
int a, b;
public Line(int a, int b) {
this.a = a;
this.b = b;
}
}
public class Main {
static int n;
static Line[] lines = new Line[500001];
static List<Line> ary;
static List<Integer> result = new ArrayList<>();
static int[] index = new int[500001];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
ary = new ArrayList<>();
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
lines[a] = new Line(a, b);
}
for (int i = 0; i < 500001; i++) {
if (lines[i] == null) continue;
if (ary.isEmpty() || ary.get(ary.size() - 1).b < lines[i].b) {
ary.add(lines[i]);
index[lines[i].a] = ary.size() - 1;
}
if (!(ary.isEmpty() || ary.get(ary.size() - 1).b < lines[i].b)) {
int idx = getCorrectIndex(0, ary.size() - 1, lines[i].b);
ary.set(idx, lines[i]);
index[lines[i].a] = idx;
}
}
int limit = ary.size() - 1;
for (int i = 500000; i >= 0; i--) {
if (lines[i] == null) continue;
if (index[i] == limit) {
limit--;
} else {
result.add(lines[i].a);
}
}
System.out.println(n - ary.size());
Collections.reverse(result);
for (int res : result) {
System.out.println(res);
}
}
public static int getCorrectIndex(int left, int right, int value) {
while (left < right) {
int mid = (left + right) / 2;
if (ary.get(mid).b >= value) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}