Coding - Algo/Java
[백준] 13335번:트럭 (Java 자바)
jainn
2021. 11. 1. 15:22
728x90
문제
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
풀이 및 소스코드
트럭과 다리를 모두 큐에 담아서 차근차근 구현해내면 된다!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
// n : 트럭의 수
int w = Integer.parseInt(st.nextToken());
// w : 다리의 길이
int l = Integer.parseInt(st.nextToken());
// l : 다리의 최대 하중
st = new StringTokenizer(br.readLine());
Queue<Integer> truck = new LinkedList<>();
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
truck.add(Integer.parseInt(st.nextToken()));
}
for(int i=0;i<w;i++) {
q.add(0);
}
// 다리 길이만큼 0 담기
int w_sum = 0;
// 다리위에 올라간 트럭의 합
int time = 0;
while (true) {
time++;
w_sum -= q.poll();
if (!truck.isEmpty()) {
if (truck.peek() + w_sum <= l) {
w_sum += truck.peek();
q.add(truck.poll());
}else {
q.add(0);
}
}
if(q.isEmpty()) break;
}
System.out.println(time);
}
}
반응형