슈콩

[BOJ] 백준 8980 택배 본문

Algorithms/Baekjoon

[BOJ] 백준 8980 택배

shukong 2025. 9. 5. 14:22

[문제]

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

 

 

[소스 코드]

import java.io.*;
import java.util.*;
public class Main {
	static class Deliver{
		int from,to,box;
		Deliver(int from,int to,int box){
			this.from = from;
			this.to = to;
			this.box = box;
		}
	}
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer st = new StringTokenizer(br.readLine());
    	int n = Integer.parseInt(st.nextToken());
    	int[] truck = new int[n+1];
    	int c = Integer.parseInt(st.nextToken());
    	int m = Integer.parseInt(br.readLine());
    	List<Deliver> list = new ArrayList<>();
    	for(int i=0;i<m;i++) {
    		st = new StringTokenizer(br.readLine());
    		int from = Integer.parseInt(st.nextToken());
    		int to = Integer.parseInt(st.nextToken());
    		int box = Integer.parseInt(st.nextToken());
    		list.add(new Deliver(from,to,box));
    	}
    	list.sort((a,b)->{
    		return a.to - b.to;
    	});
    	int result = 0;
    	for(Deliver d : list) {
    		int canLoad = d.box;
    		for(int i=d.from;i<d.to;i++) {
    			canLoad = Math.min(canLoad, c - truck[i]);
    		}
    		if(canLoad>0) {
    			for(int i=d.from;i<d.to;i++) {
        			truck[i] += canLoad;
        		}
    		}
    		result += canLoad;
    	}
    	System.out.println(result);
    }
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 6064 카잉 달력  (0) 2025.09.05
[BOJ] 백준 11653 소인수분해  (0) 2025.09.05
[BOJ] 백준 7570 줄 세우기  (0) 2025.09.04
[BOJ] 백준 2457 공주님의 정원  (0) 2025.09.04
[BOJ] 백준 2457 공주님의 정원  (0) 2025.09.03