이 문제는 시물레이션 문제이다.
나는 Queue를 사용해서 구현했다.
그리고 큐에서 peek()를 통해 맨 처음 들어간 트럭 시간을 통해 트럭시간 + 다리길이 == 현재시간 이면 poll()했다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
class Truck{
int weight;
int time;
public Truck(int weight, int time){
this.weight = weight;
this.time = time;
}
@Override
public String toString() {
return "Truck{" +
"weight=" + weight +
", time=" + time +
'}';
}
}
public class Algorithm {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] arr = br.readLine().split(" ");
int n = Integer.parseInt(arr[0]);
int w = Integer.parseInt(arr[1]);
int l = Integer.parseInt(arr[2]);
arr = br.readLine().split(" ");
Queue<Truck> queue = new LinkedList<>();
int time = 0;
int truckIdx = 0;
int truckSum = 0;
while(true){
time++;
if(!queue.isEmpty()){
Truck queueElement = queue.peek();
if(queueElement.time + w == time){
queue.poll();
truckSum -= queueElement.weight;
}
}
if(truckIdx < n){
if(queue.isEmpty()){
int elementWeight = Integer.parseInt(arr[truckIdx]);
queue.add(new Truck(elementWeight, time));
truckSum += elementWeight;
truckIdx++;
} else{
int elementWeight = Integer.parseInt(arr[truckIdx]);
//queue에 들어감
if(truckSum + elementWeight <= l){
queue.add(new Truck(elementWeight, time));
truckSum += elementWeight;
truckIdx++;
}
}
}
if(truckIdx == n && queue.size() == 0){
break;
}
}
System.out.println(time);
}
}
정답률이 높은데 생각보다 어렵게 풀었던 것 같다.
시물레이션 문제를 더 연습해야겠다!