https://www.acmicpc.net/problem/13335
13335번: 트럭
입력 데이터는 표준입력을 사용한다. 입력은 두 줄로 이루어진다. 입력의 첫 번째 줄에는 세 개의 정수 n (1 ≤ n ≤ 1,000) , w (1 ≤ w ≤ 100) and L (10 ≤ L ≤ 1,000)이 주어지는데, n은 다리를 건너는 트
www.acmicpc.net
저는 구현을 통해서 문제를 해결하였습니다.
저는 큐를 통해 다리에 존재하는 트럭을 추적하였습니다.
현재 다리에 있는 무게를 추적하고 트럭이 들어갈 수 있는지를 확인하고 들어가지 못한다면 들어갈 수 있을 때까지 큐에서 트럭을 빼고 들어가야할 트럭을 넣었습니다.
마지막으로 들어가있는 트럭을 모두 빼면서 시간을 추적하였습니다.
package BOJ.etc;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ_13335 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
int l = Integer.parseInt(st.nextToken());
int[] input = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
input[i] = Integer.parseInt(st.nextToken());
}
int result = cal(input, n, w, l);
System.out.println(result);
}
private static int cal(int[] input, int n, int w, int l) {
Queue<int[]> q = new LinkedList<>(); // int[] {무게,시간}
int weight = 0;
int time = 0;
for (int i = 0; i < n; i++) {
while (!q.isEmpty() && weight + input[i] > l) {
int[] now = q.poll();
weight -= now[0];
time = Math.max(time,now[1] + w - 1);
}
time++;
q.offer(new int[]{input[i], time});
weight += input[i];
}
while (!q.isEmpty()) {
int[] now = q.poll();
time = now[1] + w;
}
return time;
}
}
'알고리즘' 카테고리의 다른 글
벡즌 17090번 미로 탈출하기 (JAVA) (0) | 2022.08.07 |
---|---|
백준 1713번 후보 추천하기 (JAVA) (0) | 2022.08.06 |
백준 5557번 1학년 (JAVA) (0) | 2022.08.04 |
배준 19621번 회의실 배정2 (JAVA) (0) | 2022.08.03 |
백준 5430번 AC (JAVA) (0) | 2022.08.02 |