본문 바로가기
PS 문제 풀이/Baekjoon

[BOJ] 백준 11651 좌표 정렬하기 2

by 까망 하르방 2021. 2. 26.
반응형

출처 https://www.acmicpc.net/problem/11651

 Input 

5

0 4

1 2

1 -1

2 2

3 3

 

 Output 

1 -1

1 2

2 2

3 3

0 4

- 우선순위 큐와 Comparable을 이용해 구현

 


import java.io.*;
import java.util.*;
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;
        
        int N = Integer.parseInt(br.readLine());
        
        PriorityQueue<Point> pq = new PriorityQueue<>();
        for(int i=0; i<N; i++) {
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            pq.add(new Point(x, y));
        }
 
        while(!pq.isEmpty())
            System.out.println(pq.poll());
    }
}
 
class Point implements Comparable<Point>{
    int x, y;
 
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
 
    @Override
    public String toString() {
        return x + " " + y;
    }
 
    @Override
    public int compareTo(Point target) {
        if(this.y != target.y ) return this.y - target.y;
        return this.x - target.x;
    }  
}

 

반응형

댓글