01-08 08:57
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_49 ์ขŒํ‘œ ์ •๋ ฌ ๋ณธ๋ฌธ

Algorithm/Java

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_49 ์ขŒํ‘œ ์ •๋ ฌ

miinsun 2022. 1. 12. 17:57

 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…

N๊ฐœ์˜ ํ‰๋ฉด์ƒ์˜ ์ขŒํ‘œ(x, y)๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๋ชจ๋“  ์ขŒํ‘œ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
์ •๋ ฌ๊ธฐ์ค€์€ ๋จผ์ € x๊ฐ’์˜ ์˜ํ•ด์„œ ์ •๋ ฌํ•˜๊ณ , x๊ฐ’์ด ๊ฐ™์„ ๊ฒฝ์šฐ y๊ฐ’์— ์˜ํ•ด ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿ”จ ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ - ์ฒซ์งธ ์ค„์— ์ขŒํ‘œ์˜ ๊ฐœ์ˆ˜์ธ N(3<=N<=100,000)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ขŒํ‘œ๊ฐ€ x, y ์ˆœ์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. x, y๊ฐ’์€ ์–‘์ˆ˜๋งŒ ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค.

5
2 7
1 3
1 2
2 5
3 6

์ถœ๋ ฅ - N๊ฐœ์˜ ์ขŒํ‘œ๋ฅผ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜์„ธ์š”.

1 2
1 3
2 5
2 7
3 6

 

 

โ€‹

๐Ÿ’ป Solution.java

import java.util.*;

class Point implements Comparable<Point>{
	public int x, y;
	Point (int x, int y){
		this.x = x;
		this.y = y;
	}
	
	@Override
	public int compareTo(Point p) {
		if(this.x == p.x) return this.y - p.y;
		else return this.x - p.x;
	}
}

public class Main {
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		ArrayList<Point> arr = new ArrayList<Point>();
		
		for(int i = 0; i < n; i++) {
			int x = sc.nextInt();
			int y = sc.nextInt();
			arr.add(new Point(x,y));
		}
		
		Collections.sort(arr);
		for(Point p : arr) {
			System.out.println(p.x + " " + p.y);
		}
		sc.close();
		return ;
	}
}

 

 

 

Comments