์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step12] 11650 : ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 31. 16:23

www.acmicpc.net/problem/11650

 

11650๋ฒˆ: ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— ์ ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” i๋ฒˆ์ ์˜ ์œ„์น˜ xi์™€ yi๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (-100,000 ≤ xi, yi ≤ 100,000) ์ขŒํ‘œ๋Š” ํ•ญ์ƒ ์ •์ˆ˜์ด๊ณ , ์œ„์น˜๊ฐ€ ๊ฐ™์€ ๋‘ ์ ์€ ์—†๋‹ค.

www.acmicpc.net


๋ฌธ์ œ

2์ฐจ์› ํ‰๋ฉด ์œ„์˜ ์  N๊ฐœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ขŒํ‘œ๋ฅผ x์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์œผ๋กœ, x์ขŒํ‘œ๊ฐ€ ๊ฐ™์œผ๋ฉด y์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ •๋ ฌํ•œ ๋‹ค์Œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” i๋ฒˆ์ ์˜ ์œ„์น˜ xi์™€ yi๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (-100,000 ≤ xi, yi ≤ 100,000) ์ขŒํ‘œ๋Š” ํ•ญ์ƒ ์ •์ˆ˜์ด๊ณ , ์œ„์น˜๊ฐ€ ๊ฐ™์€ ๋‘ ์ ์€ ์—†๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ ์„ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 

5

3 4

1 1

1 -1

2 2

3 3

 

์˜ˆ์ œ ์ถœ๋ ฅ 

1 -1

1 1

2 2

3 3

3 4


ํ’€์ด

๋‚˜๋Š”.. ์ด๊ฒŒ ์ •๋ ฌ ํŒŒํŠธ์— ์žˆ๊ณ .. ์•ž์— ์—ฌ๋Ÿฌ ์ •๋ ฌ๋“ค์„ ์“ฐ๋ผ๊ณ  ํ•ด์„œ.. ๊ทธ๊ฒƒ๋“ค์„ ์ด์šฉํ•ด์„œ ํ‘ธ๋Š”์ค„ ์•Œ๊ณ ......

x์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  x์ขŒํ‘œ๊ฐ€ ๊ฐ™์€ ๊ฒƒ๋“ค๋ผ๋ฆฌ ๋‹ค์‹œ y์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€๋ ค๊ณ  ์•„์ฃผ ๋จธ๋ฆฌ ์ฅ์–ด์‹ธ๋งค๋ฉฐ ์ฝ”๋”ฉํ–ˆ๋Š”๋ฐ..

ํ’€๊ณ  ๋‚œ ๋’ค์— ์ฐพ์•„๋ณด๋‹ˆ comparator๋ฅผ ์žฌ์ •์˜ํ•ด์„œ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋”๋ผ ํ•˜ํ•˜

compare ์•Œ์•˜๋Š”๋ฐ ์™œ ๋– ์˜ฌ๋ฆฌ์ง€ ๋ชปํ–ˆ๋Š”์ง€..?

์ผ๋‹จ ๋จธ๋ฆฌ ์ฅ์–ด์‹ธ๋งจ ์‹œ๊ฐ„์ด ์•„๊นŒ์šฐ๋‹ˆ ๋‚ด๊ฐ€ ํ‘ผ ๋ฉ์ฒญํ•œ ๋ฐฉ๋ฒ•๋„ ํ•จ๊ป˜ ๊ธฐ๋กํ•ด๋‘”๋‹ค..

 

- ๋ฉ์ฒญํ•œ ๋ฐฉ๋ฒ•

๊ธฐ๋ณธ ํ•ฉ๋ณ‘์ •๋ ฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ 2์ฐจ์› ๋ฐฐ์—ด์„ ๊ฐ๊ฐ x๋ฅผ ๊ธฐ์ค€์œผ๋กœ, y๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋„๋ก ํ•œ๋‹ค.

1. int[n][2] ํฌ๊ธฐ์˜ ๋ฐฐ์—ด xy๋ฅผ ์„ ์–ธ, ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ x๋Š” xy[0]์—, y๋Š” xy[1]์— ์ €์žฅํ•œ๋‹ค

2. ๋ฐฐ์—ด xy๋ฅผ x๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค

3. x๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ x๊ฐ€ ๊ฐ™์€ ๊ฒƒ๋ผ๋ฆฌ ๋ฌถ์–ด์„œ y๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค.

   3-1. left = 0, right = 0์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ xy[i][0] ์™€ xy[i+1][0] ์„ ๋น„๊ต

   3-2. ๊ฐ™์ง€ ์•Š์œผ๋ฉด right์— i๋ฅผ ์ €์žฅํ•œ ํ›„ 0 ~ i ์‚ฌ์ด๋ฅผ y๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.

   3-3. left์— i+1์„ ์ €์žฅํ•œ ํ›„ ๋ฐ˜๋ณต

   3-4. for๋ฌธ ์ˆ˜ํ–‰์ด ๋๋‚˜๋ฉด left~๋ฐฐ์—ด์˜ ๋๊นŒ์ง€ y๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.

          ( ๋งˆ์ง€๋ง‰์—” xy[i][0] ์™€ xy[i+1][0] ๊ฐ€ ๋‹ค๋ฅธ ๋ถ€๋ถ„์„ ๋งŒ๋‚˜์ง€ ๋ชปํ•˜๊ณ  ๋๋‚˜ y์ •๋ ฌ ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— )

4. ์ •๋ ฌ๋œ ๋ฐฐ์—ด xy ์ถœ๋ ฅ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class B11650_1 {
	
	static int[][] buff;
	
	static void mergeSortX(int[][] a, int left, int right) {
		if(left < right) {
			int i;
			int center = (left + right) /2;
			int p = 0;
			int j = 0;
			int k = left;
			
			mergeSortX(a, left, center);
			mergeSortX(a, center + 1, right);
			
			for(i = left; i <= center; i++) {
				buff[p][0] = a[i][0];
				buff[p++][1] = a[i][1];
			}

			while(i <= right && j < p) {
				a[k][0] = (buff[j][0] <= a[i][0]) ? buff[j][0] : a[i][0];
				a[k++][1] = (buff[j][0] <= a[i][0]) ? buff[j++][1] : a[i++][1];
			}
					
			while(j < p) {
				a[k][0] = buff[j][0];
				a[k++][1] = buff[j++][1];
			}
		}
	}
	
	static void mergeSortY(int[][] a, int left, int right) {
		if(left < right) {
			int i;
			int center = (left + right) /2;
			int p = 0;
			int j = 0;
			int k = left;
			
			mergeSortY(a, left, center);
			mergeSortY(a, center + 1, right);
			
			for(i = left; i <= center; i++) {
				buff[p][0] = a[i][0];
				buff[p++][1] = a[i][1];
			}

			while(i <= right && j < p) {
				a[k][0] = (buff[j][1] <= a[i][1]) ? buff[j][0] : a[i][0];
				a[k++][1] = (buff[j][1] <= a[i][1]) ? buff[j++][1] : a[i++][1];
			}
					
			while(j < p) {
				a[k][0] = buff[j][0];
				a[k++][1] = buff[j++][1];
			}
		}
	}
	
	static void mergeSort(int[][] a, int n) {
		buff = new int[n][2];
		mergeSortX(a, 0, n-1);
		
		int left = 0, right = 0;
		for(int i = 0; i < n-1; i++) {
			if(a[i][0] != a[i+1][0]) {
				right = i;
				mergeSortY(a, left, right);
				left = i+1;
			}
		}
		mergeSortY(a, left, n-1);
		buff = null;
	}

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(br.readLine());
		int[][] xy = new int[n][2];
		
		StringTokenizer st;
		for(int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			xy[i][0] = Integer.parseInt(st.nextToken());
			xy[i][1] = Integer.parseInt(st.nextToken());
		}
		
		mergeSort(xy, n);
		
		for(int i = 0; i < n; i++) {
			System.out.println(xy[i][0] + " " + xy[i][1]);
		}
	}
}

 

- comparator ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•

import java.io.*;
import java.util.*;

public class B11650_2 {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(br.readLine());
		int[][] xy = new int[n][2];
		
		StringTokenizer st;
		for(int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			xy[i][0] = Integer.parseInt(st.nextToken());
			xy[i][1] = Integer.parseInt(st.nextToken());
		}
		
		Arrays.sort(xy, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				if(o1[0] == o2[0]){
		            return o1[1] - o2[1];
		        }else{
		            return o1[0] - o2[0];
		        }
			}
		});
		
		for(int i = 0; i < n; i++) {
			System.out.println(xy[i][0] + " " + xy[i][1]);
		}
	}
}