https://www.acmicpc.net/problem/13335
접근 :
- 다리의 길이 만큼 큐를 만든다.
- 다리가 트럭을 더 지탱할 수 있으면 트럭을 큐에 집어 넣고 만약 지탱할 수 없으면 0 을 넣는다.
- 마지막 트럭까지 다리에 넣은 후, 마지막 트럭이 다리를 빠져나올 때 까지 시간을 기록 후 출력한다.
코드구현 :
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] temp = br.readLine().split(" ");
int n = Integer.parseInt(temp[0]);
int w = Integer.parseInt(temp[1]);
int l = Integer.parseInt(temp[2]);
int[] trucks = new int[n];
String[] inputs = br.readLine().split(" ");
for(int i= 0 ; i < n ; i++) trucks[i] = Integer.parseInt(inputs[i]);
Queue<Integer> queue = new LinkedList<>();
for(int j = 0 ; j < w ; j++) queue.add(0);
int answer = 0;
int total_weight = 0;
int truck_num = 0;
while(!queue.isEmpty()){
answer++;
total_weight -= queue.poll();
if(truck_num == n) continue;
int next = (total_weight + trucks[truck_num] > l) ? 0 : trucks[truck_num++];
queue.offer(next);
total_weight += next;
}
System.out.println(answer);
br.close();
}
}
'JAVA > Coding Test' 카테고리의 다른 글
[JAVA] 백준 13144 List of Unique Numbers_큐 (1) | 2024.06.07 |
---|---|
[JAVA] 백준 2002 추월_반복문(포인터 설정) (0) | 2024.06.07 |
[JAVA] 백준 2343 기타 레슨_이분탐색 (0) | 2024.06.05 |
[JAVA] 백준 2512 예산_이분탐색 (0) | 2024.06.05 |
[JAVA] 백준 1654 랜선자르기_이분탐색 (0) | 2024.06.04 |