[BOJ 8980] ํƒ๋ฐฐ

 

1. ๋ฌธ์ œ : https://www.acmicpc.net/problem/8980

 

2. ํ’€์ด

https://maivve.tistory.com/36

 

3. ์ฝ”๋“œ

package baekjoon;

import java.util.*;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int truck = sc.nextInt();
        int m = sc.nextInt();

        int[] boxs = new int[n + 1]; // ์ธ๋ฑ์Šค : 1 ~ n๊นŒ์ง€. ํ•ด๋‹น index ๋งˆ์„์— ๋„์ฐฉํ–ˆ์„ ๋•Œ์˜ ํŠธ๋Ÿญ์— ๋‹ด์€ ๋ฐ•์Šค ๊ฐœ์ˆ˜
        ArrayList<Town> towns = new ArrayList<>();

        for (int i = 0; i < m; i++) {
            int from = sc.nextInt();
            int to = sc.nextInt();
            int box = sc.nextInt();
            towns.add(new Town(from, to, box));
        }

        Collections.sort(towns);    // ๋ฐ›๋Š” ๋งˆ์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

        int boxCount = 0;
        
        for (Town town : towns) {
            int start = town.start;
            int end = town.end;
            int box = town.box;

            int max = 0;
            boolean isLoad = true;
            for (int i = start; i < end; i++) {
                if (boxs[i] >= truck) {
                    isLoad = false;
                    break;
                }
                max = Math.max(max, boxs[i]);
            }

            if (isLoad) {
                int unloads = truck - max;
                if (unloads > box) {
                    unloads = box;
                }
                boxCount += unloads;

                for (int i = start; i < end; i++) {
                    boxs[i] += unloads;
                }
            }
        }
        System.out.println(boxCount);
    }
}

class Town implements Comparable<Town> {
    int start;
    int end;
    int box;

    Town(int start, int end, int box) {
        this.start = start;
        this.end = end;
        this.box = box;
    }

    // ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ์œ„ํ•œ Comparable ํด๋ž˜์Šค ํ•จ์ˆ˜ ์‚ฌ์šฉ
    @Override
    public int compareTo(Town town) {
        if (this.end < town.end) {
            return -1;
        } else if (this.end == town.end) {
            return 0;
        } else {
            return 1;
        }
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ