JAVA/Coding Test

[JAVA] 백준 13335 트럭_큐

오늘도개발 2024. 6. 6. 20:16

 

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();
    }
}