01-10 04:27
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ์ •๋ ฌ 11650 :: ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ JAVA ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ์ •๋ ฌ 11650 :: ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ JAVA

miinsun 2022. 3. 14. 00:44

 

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

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

 

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

์ž…๋ ฅ 

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

 

์ถœ๋ ฅ

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

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

5
3 4
1 1
1 -1
2 2
3 3

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

1 -1
1 1
2 2
3 3
3 4

 

 

โ€‹

๐Ÿ’ป  Main.java

  • ์•„๋ž˜ ๊ฒŒ์‹œ๊ธ€๊ณผ ๊ฐ™์€ ๋ฌธ์ œ
  • Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ compareTo ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์ค€๋‹ค.
  • x์˜ ํฌ๊ธฐ๋ฅผ ๋จผ์ € ๋น„๊ตํ•˜๊ณ , x์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์œผ๋ฉด y์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•œ๋‹ค.
  • Collections.sort ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ •๋ ฌํ•œ๋‹ค.
 

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

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

miinsun.tistory.com

/* ๋ฐฑ์ค€ ์ •๋ ฌ - 11650 :: ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

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 o) {
		if(this.x  == o.x) return this.y - o.y;
		return this.x - o.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();
	}
}
Comments