๋ฌธ์ œ ํ•ด๊ฒฐ (PS)/์ฝ”ํ…Œ TIL

[Java] ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ | ๋ฐฑ์ค€ 2457 ๊ณต์ฃผ๋‹˜์˜ ์ •์›

9taetae9 2024. 11. 2. 10:58
728x90

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

 

์ ‘๊ทผ ๋ฐฉ๋ฒ•

 

3์›” 1์ผ๋ถ€ํ„ฐ 11์›” 30์ผ๊นŒ์ง€ ๋งค์ผ ๊ฝƒ์ด ํ•œ ๊ฐ€์ง€ ์ด์ƒ ํ”ผ์–ด์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ 

๊ฝƒ ํ”ผ๋Š” ๋‚ ์งœ ์ค‘ ํ•˜๋‚˜๋Š” 3์›” 1์ผ ์ดํ•˜, ์ง€๋Š” ๋‚ ์งœ ์ค‘ ํ•˜๋‚˜๋Š” ๋ฌด์กฐ๊ฑด 12์›” ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.

3์›” 1์ผ ์ดํ•˜ ๋‚ ์งœ์— ํ”ผ๋Š” ๊ฝƒ๋“ค ์ค‘ ์–ด๋–ค ๊ฝƒ์„ ๊ณจ๋ผ์•ผ ๋ ์ง€ ์ƒ๊ฐํ•ด๋ณด๋ฉด, ๊ทธ ์ค‘์—์„œ๋Š” ๋ฌด์กฐ๊ฑด ๊ฐ€์žฅ ๋Šฆ๊ฒŒ ์ง€๋Š” ๊ฝƒ์„ ๊ณ ๋ฅด๋Š” ๊ฒƒ์ด ๊ฝƒ๋“ค์˜ ์ˆ˜๋ฅผ ๊ฐ€๋Šฅํ•œ ์ ๊ฒŒ ํ•˜๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ ์œ ๋ฆฌํ•˜๋‹ค.

 

๊ทธ๋ ‡๊ฒŒ ์ฒ˜์Œ ์„ ํƒํ•  ๊ฝƒ์„ ์„ ์ •ํ–ˆ๋‹ค๋ฉด, ๊ทธ ์„ ํƒ๋œ ๊ฝƒ์˜ ์ง€๋Š” ๋‚ ์งœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‹ค์Œ ์„ ํƒ๋  ๊ฝƒ์„ ์„ ์ •ํ•˜๋ฉด ๋  ๊ฒƒ์ด๋‹ค.

๊ตฌ๊ฐ„์„ ๊ฒน์น  ์ˆ˜ ์žˆ๊ฒŒ ๋ฝ‘๋˜, ์ง€๋Š” ๋‚ ์งœ๊ฐ€ ๊ธด ๊ฝƒ์„ ์„ ํƒ.

 

๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์นด์šดํŠธ๋ฅผ ํ•˜๊ณ , ์ง€๋Š” ๋‚ ์งœ๊ฐ€ 12์›” ์ด์ƒ์ธ ๊ฝƒ์„ ์„ ํƒํ–ˆ์„ ๋•Œ ๋ฐ˜๋ณต์„ ์ข…๋ฃŒํ•˜๊ณ  ์นด์šดํŠธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

(๊ทธ ์ด์ „์— ์„ ํƒ ๊ทœ์น™์— ์ ํ•ฉํ•œ ๊ฝƒ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ฆ‰์‹œ 0์„ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค.)

 

์ด ๊ณผ์ •์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ๊ฝƒ์„ ์‹œ์ž‘์ผ ๊ธฐ์ค€์œผ๋กœ ์šฐ์„  ๋‚˜์—ดํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ Flower ํด๋ž˜์Šค ๋‚ด์— ์‹œ์ž‘์ผ ๊ธฐ์ค€์œผ๋กœ ๊ฝƒ์„ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋„๋ก Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

static class Flower implements Comparable<Flower>{
    int[] start = new int[2];
    int[] end = new int[2];

    public Flower(int start_m, int start_d, int end_m, int end_d){
        this.start[0] = start_m;
        this.start[1] = start_d;
        this.end[0] = end_m;
        this.end[1] = end_d;
    }

    //์‹œ์ž‘์ผ ๊ธฐ์ค€
    @Override
    public int compareTo(Flower other){
        if(this.start[0] != other.start[0]){
            return Integer.compare(this.start[0], other.start[0]);
        }else if(this.start[1]!=other.start[1]){
            return Integer.compare(this.start[1], other.start[1]);
        }else if(this.end[0]!=other.end[0]){
            return Integer.compare(other.end[0], this.end[0]);
        }else{
            return Integer.compare(other.end[1], this.end[1]);
        }
    }
}

 

์•„๋ž˜์™€ ๊ฐ™์ด main ๋ฉ”์„œ๋“œ์—์„œ ๊ฝƒ์„ ์ž…๋ ฅ ๋ฐ›๊ณ  ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด ์ค€ ๋’ค ์‹œ๊ฐ„์ผ ๊ธฐ์ค€์œผ๋กœ ๋‚˜์—ด์‹œํ‚จ๋‹ค.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());

List<Flower> flowers = new ArrayList<>();

StringTokenizer st;
for(int i=0; i < N; i++){
    st = new StringTokenizer(br.readLine());
    int start_m = Integer.parseInt(st.nextToken());
    int start_d = Integer.parseInt(st.nextToken());
    int end_m = Integer.parseInt(st.nextToken());
    int end_d = Integer.parseInt(st.nextToken());
    flowers.add(new Flower(start_m,start_d, end_m, end_d));
}

Collections.sort(flowers);

 

๋‹ค์Œ์œผ๋กœ ํ˜„์ œ ๊ธฐ์ค€์ด ๋˜๋Š” ๋‚ ์งœ 3์›” 1์ผ์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜ currentMonth, currentDay๋ฅผ ์„ ์–ธํ•ด ์ฃผ์—ˆ๊ณ , ๊ฝƒ์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ index ๋ณ€์ˆ˜, ๊ฝƒ ๊ฐœ์ˆ˜๋ฅผ ์ƒˆ๊ธฐ ์œ„ํ•œ count ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ–ˆ๋‹ค.

int currentMonth = 3; int currentDay = 1; int index = 0; int count = 0; // ๊ฝƒ ๊ฐœ์ˆ˜

 

๋งˆ์ง€๋ง‰์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฒ˜์Œ ์ƒ๊ฐํ–ˆ๋˜ ๊ฝƒ์˜ ์„ ํƒ๊ณผ์ •์„ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. ์‹œ์ž‘์ผ์ด ํ˜„์žฌ ๋‚ ์งœ ์ดํ•˜์ด๋ฉด์„œ ๊ฐ€์žฅ ๋Šฆ๊ฒŒ ์ง€๋Š” ๊ฝƒ์„ ์„ ํƒํ•˜๋Š” ๊ณผ์ •์ด๋‹ค. ์—ฌ๊ธฐ์„œ isLaterDate ๋ฉ”์„œ๋“œ๊ฐ€ ๊ฐ€์žฅ ๋Šฆ๊ฒŒ ์ง€๋Š” ๊ฝƒ์„ ๊ฐฑ์‹ ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š”๋ฐ ๋งŒ์•ฝ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ํ•œ๋ฒˆ๋„ ํ˜ธ์ถœ์ด ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์‹œ์ž‘์ผ์ด ํ˜„์žฌ ๋‚ ์งœ ์ดํ•˜์ด๋ฉด์„œ ์ง€๋Š” ๋‚ ์งœ๊ฐ€ ์‹œ์ž‘์ผ ์ดํ›„์ธ ๊ฝƒ์ด ํ•˜๋‚˜๋„ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ์•Š์•„ ์ข…๋ฃŒ์‹œ์ผœ์ฃผ์–ด์•ผํ•œ๋‹ค. ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด found ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ์„ ํƒ๋œ maxEndMoth, maxEndDay(์ข…๋ฃŒ์ผ์ด ๊ฐ€์žฅ ๋’ค์ธ ๊ฝƒ)์„ currentMonth, currentDay๋กœ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ์ด๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.

while (currentMonth < 12) {
        int maxEndMonth = currentMonth;
        int maxEndDay = currentDay;
        boolean found = false;

        while(index < flowers.size() &&
                (flowers.get(index).start[0] < currentMonth ||
                (flowers.get(index).start[0] == currentMonth &&
                       flowers.get(index).start[1] <= currentDay))){

            Flower current = flowers.get(index);
            //๋” ๋Šฆ์œผ๋ฉด ๊ฐฑ์‹ 
            if(isLaterDate(current.end[0], current.end[1], maxEndMonth, maxEndDay)){
                maxEndMonth = current.end[0];
                maxEndDay = current.end[1];
                found = true;
            }

            index++;

        }

        if(!found){
            System.out.println(0);
            return;
        }

        currentMonth = maxEndMonth;
        currentDay = maxEndDay;
        count++;

    }

    System.out.println(count);
}
private static boolean isLaterDate(int m1, int d1, int m2, int d2){
    if(m1 != m2){
        return m1 > m2;
    }
    return d1 > d2;
}

 

 

๋А๋‚€์  :

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์ ‘๊ทผ ๋ฐฉ๋ฒ•๋ณด๋‹ค ๋‚˜์˜ ์ƒ๊ฐ์„ ์ •ํ™•ํžˆ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ •์—์„œ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋Š”๋ฐ, ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ๋ชป ํ•  ๊ฒฝ์šฐ 0์„ ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š” ๊ฒฝ๊ณ„ ์กฐ๊ฑด๋“ค์„ ์ฒดํฌํ•ด์•ผ ํ–ˆ๋˜ ์ ์ด ์ค‘์š”ํ–ˆ๋‹ค. ๋˜ํ•œ ์‹œ์ž‘ ๋‚ ์งœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚˜์—ดํ•œ ๊ฒƒ์ด ๋งค ์„ ํƒ๋งˆ๋‹ค ๋ชจ๋“  ๊ฝƒ์„ ํ™•์ธํ•˜์ง€ ์•Š๊ณ  ํ˜„์žฌ ๋‚ ์งœ ์ดํ•˜์— ํ”ผ๋Š” ๊ฝƒ๋“ค๋งŒ ๊ฒ€ํ† ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ฃผ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ทธ ํ˜„์žฌ ์‹œ์ ์—์„œ์˜ ์ตœ์„ ์˜ ์„ ํƒ์ด ์ „์ฒด์ ์œผ๋กœ๋„ ์ตœ์„ ์ด ๋˜๋Š” ๊ตฌ์กฐ๋กœ, ๋‹ค์‹œ ๋ฐฑํŠธ๋ž˜ํ‚นํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŠน์„ฑ์— ๋Œ€ํ•œ ์ดํ•ด๋„ ์ž˜ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™๋‹ค.

728x90