ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Algorithm/Baekjoon

[Java] ๋ฐฑ์ค€ 1931๋ฒˆ - ํšŒ์˜์‹ค ๋ฐฐ์ •

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 6. 13. 21:07

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

 

1931๋ฒˆ: ํšŒ์˜์‹ค ๋ฐฐ์ •

(1,4), (5,7), (8,11), (12,14) ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

www.acmicpc.net

[๋ฌธ์ œ]

 

 

[ํ’€์ด]

 

์ฒ˜์Œ์—” "์‹œ์ž‘์‹œ๊ฐ„"๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ›„, ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ํšŒ์˜ ์กฐํ•ฉ์„ ๊ตฌํ•˜์—ฌ max๊ฐ’์„ ๋ฝ‘์•„๋‚ด๋ ค ํ–ˆ์œผ๋‚˜, ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์งˆ๋ฌธํ•˜๊ธฐ์˜ ๋„์›€์„ ๋ฐ›์•„ "์ข…๋ฃŒ์‹œ๊ฐ„"๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ํ‘ธ๋Š” ๋ฌธ์ œ๋ผ๋Š” ํžŒํŠธ๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค.

 

- Compatable<>์„ ์ด์šฉํ•˜์—ฌ ์ข…๋ฃŒ์‹œ๊ฐ„์œผ๋กœ ์ •๋ ฌํ•˜๋ฉฐ, **๋งŒ์•ฝ ์ข…๋ฃŒ์‹œ๊ฐ„์ด ๊ฐ™๋‹ค๋ฉด start์‹œ๊ฐ„์ด ์งง์€์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ

1 4
3 5
0 6
5 7
3 8
5 9
6 10
8 11
8 12
2 13
12 14

 

(1,4) -> time: 4 ์ด๋ฏ€๋กœ 4๋ณด๋‹ค ์‹œ์ž‘์‹œ๊ฐ„์ค‘ ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€๊ฐ’์ด ๋‚˜์˜ค๋ฉด ๊ฐฑ์‹  -> (5,7) -> (8,11) -> (12,14) 

 

** end ์‹œ๊ฐ„์ด ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ๋ชปํ•ด 88%์—์„œ ํ‹€๋ ค์„œ ์งˆ๋ฌธํ•˜๊ธฐ์— ์˜ฌ๋ผ์˜จ ๋ฐ˜๋ก€๋ฅผ ํ†ตํ•ด ๋ฐœ๊ฒฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋ฐ˜๋ก€
5
6 7
6 6
5 6
5 5
7 7

์ •๋‹ต : 5
์ถœ๋ ฅ : 4

 

 

[์ฝ”๋“œ]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

class Room implements Comparable<Room>{
    int start;
    int end;

    public Room(int start, int end) {
        this.start = start;
        this.end = end;
    }
    @Override
    public int compareTo(Room o) {
        if(this.end == o.end) {
            return this.start - o.start;
        }
        return this.end - o.end;
    }
}
public class Main {
    public static int N;
    public static Room[] room;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        room = new Room[N];
        for(int i=0;i<N;i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int start = Integer.parseInt(st.nextToken());
            int end = Integer.parseInt(st.nextToken());
            room[i] = new Room(start, end);
        }
        Arrays.sort(room);
        countRoom();
    }

    // cnt : ์ง„ํ–‰๋œ ํšŒ์˜ ์ดํ•ฉ
    public static void countRoom() {
        // ์‹œ์ž‘์€ ๋ฌด์ €๊ฑด 0 ๋ถ€ํ„ฐ์ž„
        int time = room[0].end;
        int cnt = 1;
        for(int i=1;i<N;i++) {
            if(time <= room[i].start) {
                time = room[i].end;
                cnt++;
            }
        }
        System.out.println(cnt);
    }
}