https://www.acmicpc.net/problem/11650
๐ ํ์ด 3 : ์ด์ฐจ์ ๋ฐฐ์ด ์ ๋ ฌ(thenComparing())
`Comparator` ์ธํฐํ์ด์ค์ `comparing()`, `thenComparing()` ๋ฉ์๋ ์ฌ์ฉโญ
import java.io.*;
import java.util.*;
public class Main {
// 2์ฐจ์ ํ๋ฉด ์์ ์ N๊ฐ๊ฐ ์ฃผ์ด์ง ๋, x์ขํ -> y์ขํ ์์๋ก ์ ๋ ฌ ํ ์ถ๋ ฅ
public static void main(String[] args) throws IOException {
/* [1] ์
๋ ฅ */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine()); // 2์ฐจ์ ํ๋ฉด ์ ์ ์ ๊ฐ์ N (1 ≤ N ≤ 100,000)
int[][] arrList = new int[N][2];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
arrList[i][0] = Integer.parseInt(st.nextToken());
arrList[i][1] = Integer.parseInt(st.nextToken());
}
/* [2] ์ ๋ ฌ */
Arrays.sort(arrList, Comparator.comparingInt((int[] a) -> a[0]).thenComparingInt(a -> a[1]));
// (int[] a) -> a[0] ์ด ๋๋ค์์ ๋ฐฐ์ด arr์ ๊ฐ ์์(์ฆ, int[])๋ฅผ ๋ฐ์์ ๊ทธ ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์(a[0])๋ฅผ ๋ฐํํฉ๋๋ค. ๋ฐ๋ผ์ arr์ ์์๋ค์ a[0]์ ๋ฐ๋ผ ์ ๋ ฌ๋ฉ๋๋ค.
// .thenComparingInt(a -> a[1]): ์ด ๋ถ๋ถ์ ์ถ๊ฐ์ ์ธ ์ ๋ ฌ ๊ธฐ์ค์ ์ง์ ํฉ๋๋ค.
// thenComparingInt ๋ฉ์๋๋ ์ฒซ ๋ฒ์งธ ์ ๋ ฌ ๊ธฐ์ค(์ฆ, a[0])์ด ๋์ผํ ๊ฒฝ์ฐ์ ๋ ๋ฒ์งธ ๊ธฐ์ค(์ฆ, a[1])์ ์ฌ์ฉํ์ฌ ์ ๋ ฌ์ ์ํํฉ๋๋ค.
// a -> a[1] ์ด ๋๋ค์์ ๋ฐฐ์ด a๋ฅผ ๋ฐ์์ ๊ทธ ๋ฐฐ์ด์ ๋ ๋ฒ์งธ ์์(a[1])๋ฅผ ๋ฐํํฉ๋๋ค. ์ฌ๊ธฐ์ a์ ํ์
์ ์๋ต๋์์ง๋ง, ์๋ฐ์ ํ์
์ถ๋ก ์ ์ํด int[]๋ก ์ธ์๋ฉ๋๋ค.
/* [3] ์ถ๋ ฅ */
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
sb.append(arrList[i][0] + " " + arrList[i][1]).append('\n');
}
System.out.println(sb);
}
}
๐ ํ์ด 2 : ์ด์ฐจ์ ๋ฐฐ์ด ์ ๋ ฌ(๋๋ค์)
๋๋ค์์ ์ฌ์ฉํ์ฌ Comparator ๊ตฌํ ์ฝ๋ ๊ฐ๊ฒฐํ๊ฒ ์์ฑโญ
import java.io.*;
import java.util.*;
public class Main {
// 2์ฐจ์ ํ๋ฉด ์์ ์ N๊ฐ๊ฐ ์ฃผ์ด์ง ๋, x์ขํ -> y์ขํ ์์๋ก ์ ๋ ฌ ํ ์ถ๋ ฅ
public static void main(String[] args) throws IOException {
/* [1] ์
๋ ฅ */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine()); // 2์ฐจ์ ํ๋ฉด ์ ์ ์ ๊ฐ์ N (1 ≤ N ≤ 100,000)
int[][] arrList = new int[N][2];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
arrList[i][0] = Integer.parseInt(st.nextToken());
arrList[i][1] = Integer.parseInt(st.nextToken());
}
/* [2] ์ ๋ ฌ */
Arrays.sort(arrList, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); // x์ขํ๊ฐ ๋ค๋ฅผ ๊ฒฝ์ฐ x์ขํ ์ค๋ฆ์ฐจ์
/* [3] ์ถ๋ ฅ */
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
sb.append(arrList[i][0] + " " + arrList[i][1]).append('\n');
}
System.out.println(sb);
}
}
๐ ํ์ด 1 : Map<Integer, PriorityQueue>
`PriorityQueue` ์ด์ฉโญ
import java.io.*;
import java.util.*;
public class Main {
// 2์ฐจ์ ํ๋ฉด ์์ ์ N๊ฐ๊ฐ ์ฃผ์ด์ง ๋, x์ขํ -> y์ขํ ์์๋ก ์ ๋ ฌ ํ ์ถ๋ ฅ
public static void main(String[] args) throws IOException {
/* [1] ์
๋ ฅ */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine()); // 2์ฐจ์ ํ๋ฉด ์ ์ ์ ๊ฐ์ N (1 ≤ N ≤ 100,000)
Map<Integer, PriorityQueue<Integer>> map = new TreeMap<>();
for (int i = 0; i < N; i++) {
String[] xy = br.readLine().split(" "); // i๋ฒ์ ์ ์์น xi์ yi(-100,000 ≤ xi, yi ≤ 100,000)
int x = Integer.parseInt(xy[0]);
map.computeIfAbsent(x, k -> new PriorityQueue<>()).add(Integer.parseInt(xy[1]));
// x์ขํ์ ํด๋นํ๋ PriorityQueue๊ฐ ์์ผ๋ฉด ์๋ก ์์ฑ(์ด๋ฏธ ์กด์ฌํ๋ฉด ๊ธฐ์กด ๊ฐ์ ๋ฐํ)ํ๊ณ y์ขํ ์ถ๊ฐ
}
/* [2] ์ถ๋ ฅ */
StringBuilder sb = new StringBuilder();
for (Map.Entry<Integer, PriorityQueue<Integer>> entry : map.entrySet()) {
int x = entry.getKey();
PriorityQueue<Integer> yQueue = entry.getValue();
while (!yQueue.isEmpty()) {
sb.append(x + " " + yQueue.poll() + "\n"); // for-each ๋ฌธ์ผ๋ก ์ํํ ๋๋ ์์๋ค์ด ์ ๋ ฌ๋ ์์๋ก ๋ฐํ๋์ง ์์
}
}
System.out.println(sb);
}
}