코딩테스트/백준

[BJ] 11651. 좌표 정렬하기2

jhk828 2021. 3. 28. 23:02

11651. 좌표 정렬하기2

www.acmicpc.net/problem/11651

 

11651번: 좌표 정렬하기 2

첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.

www.acmicpc.net

import java.io.*;
import java.util.*;
// 210328

public class Main_BJ_11651_좌표정렬하기2 {
	public static void main(String[] args) throws Exception {
		//System.setIn(new FileInputStream("res/input_BJ_11651_좌표정렬하기2.txt"));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = null;
		
		int N = stoi(br.readLine());
		int[][] arr =new int[N][2];
		
		int x, y;
		for(int i=0; i<N; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			x = stoi(st.nextToken());
			y = stoi(st.nextToken());
			arr[i][0] = x;
			arr[i][1] = y;
		}
	
		// y 증가, x 증가순 정려
		Arrays.sort(arr, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				if(o1[1] == o2[1]) {
					return Integer.compare(o1[0], o2[0]);
				}
				return Integer.compare(o1[1], o2[1]);
			}
		});

		// 출력
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<N; i++) {
			sb.append(arr[i][0] + " " + arr[i][1] + "\n");
		}
		System.out.println(sb.toString());
		
		br.close();
	}
	
	static int stoi(String str) {
		return Integer.parseInt(str);
	}
}