-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLv2_다리를지나는트럭.cpp
More file actions
42 lines (37 loc) · 817 Bytes
/
Lv2_다리를지나는트럭.cpp
File metadata and controls
42 lines (37 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <string>
#include <vector>
#include <queue>
using namespace std;
int solution(int bridge_length, int weight, vector<int> truck_weights) {
int answer = 0;
int cweight = 0;
int ctime = 0;
queue<pair<int, int> > q;
answer++;
cweight += truck_weights[0];
q.push(make_pair(1, truck_weights[0]));
for (int i = 1; i < truck_weights.size(); i++) {
answer++;
ctime = q.front().first;
if (answer - ctime == bridge_length) {
cweight -= q.front().second;
q.pop();
}
if (cweight + truck_weights[i] <= weight) {
cweight += truck_weights[i];
q.push(make_pair(answer, truck_weights[i]));
}
else {
i--;
}
}
while (!q.empty()) {
answer++;
ctime = q.front().first;
if (answer - ctime == bridge_length) {
cweight -= q.front().second;
q.pop();
}
}
return answer;
}