728x90

https://www.acmicpc.net/problem/11650

 

11650번: 좌표 정렬하기

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

www.acmicpc.net

 

 

 

풀이)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main_BJ_11650_좌표정렬하기 {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        PriorityQueue<int []> pq = new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if(o1[0] == o2[0])
                    return o1[1]-o2[1];
                return o1[0]-o2[0];
            }
        });
        for(int i=0; i<n; i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());

            pq.offer(new int[] {x, y});
        }


        for(int i=0; i<n; i++){
            int[] cur = pq.poll();
            System.out.println(cur[0] +" "+cur[1]);
        }
    }//main
}

정렬만 하면 되는 문제이다.

나는 priorityQueue를 사용했지만, Arrays.sort를 이용하는 방법도 있다.

 

728x90

'코테 > 백준' 카테고리의 다른 글

백준 5525 IOIOI(JAVA)  (0) 2023.02.14
백준 9019 DSLR(JAVA)  (0) 2023.02.11
백준 2667 단지번호붙이기(JAVA)  (0) 2023.02.09
백준 11286 절댓값 힙(JAVA)  (0) 2023.02.08
백준 17626 Four Squares(JAVA)  (0) 2023.02.07

+ Recent posts