https://programmers.co.kr/learn/courses/30/lessons/42583
int solution(int bridge_length, int weight, vector<int> truck_weights) {
int answer = 1;
int pointTruckIdx = 1;
int traceTruckWeight = truck_weights[0];
queue<pair<int,int>> q;
q.push({ truck_weights[0], 1 });
while (pointTruckIdx < truck_weights.size())
{
answer++;
if (q.front().second >= bridge_length) {
traceTruckWeight -= q.front().first;
q.pop();
}
if (traceTruckWeight + truck_weights[pointTruckIdx] > weight) {
for (int i = 0; i < q.size(); i++) {
auto truck = q.front();
q.pop();
truck.second++;
q.push(truck);
}
}
else {
traceTruckWeight += truck_weights[pointTruckIdx];
q.push({ truck_weights[pointTruckIdx++], 0 });
for (int i = 0; i < q.size(); i++) {
auto truck = q.front();
q.pop();
truck.second++;
q.push(truck);
}
}
}
while (!q.empty())
{
answer++;
if (q.front().second >= bridge_length) {
q.pop();
for (int i = 0; i < q.size(); i++) {
auto truck = q.front();
q.pop();
truck.second++;
q.push(truck);
}
}
else {
for (int i = 0; i < q.size(); i++) {
auto truck = q.front();
q.pop();
truck.second++;
q.push(truck);
}
}
}
return answer;
}