[BOJ 8980] ํ๋ฐฐ
1. ๋ฌธ์ : https://www.acmicpc.net/problem/8980
2. ํ์ด
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;
}
}
}
'๐ป Coding Problems Solving > Greedy' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 1092] ๋ฐฐ (0) | 2023.06.06 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์๊ฒฉ์์คํ (์๋ฐ java) (0) | 2023.06.05 |
[BOJ 2212] ์ผ์ (0) | 2023.06.05 |
[BOJ 13164] ํ๋ณต์ ์น์ (0) | 2023.05.30 |
[BOJ 1700] ๋ฉํฐํญ ์ค์ผ์ค๋ง (java) (0) | 2023.04.05 |
์ต๊ทผ๋๊ธ