📕 문제
📌 문제 링크
![](https://velog.velcdn.com/images/wowns226/post/2cefb72c-7564-4a56-9db1-9f6e9d3a16ff/image.png)
📗 접근 방식
- 큐를 사용하여 다리 위에 있는 트럭을 관리
- 다리를 건너는 과정을 시뮬레이션하면서 시간을 측정
- 다음 트럭이 다리에 진입할 수 있는지 여부를 판단하고, 진입할 수 없다면 대기
- 모든 트럭이 다리를 건널 때까지 반복
📘 코드
using System;
using System.Collections.Generic;
namespace BOJ
{
class No_13335
{
static void Main()
{
var input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int n = input[0];
int w = input[1];
int l = input[2];
int[] a = new int[n];
input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
for (int i = 0; i < n; i++)
a[i] = input[i];
Queue<int> q = new Queue<int>();
int ans = 0;
int sum = 0;
for (int i = 0; i < n; i++)
{
while (true)
{
if (q.Count == w)
sum -= q.Dequeue();
if (sum + a[i] <= l)
break;
q.Enqueue(0);
ans++;
}
q.Enqueue(a[i]);
sum += a[i];
ans++;
}
ans += w;
Console.WriteLine(ans);
}
}
}
📙 오답노트
📒 알고리즘 분류