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
43
44
| #include <string>
#include <vector>
#include <queue>
#include<iostream>
using namespace std;
int solution(int bridge_length, int weight, vector<int> truck_weights) {
int time_flow = 0;
int remain_weight = weight;
queue< pair<int,int> > tTime;
pair<int,int> temp;
for (int i=0; i< truck_weights.size(); ){
while(remain_weight >= truck_weights[i] && i<truck_weights.size()){ //insert as it can
time_flow++;
remain_weight -= truck_weights[i];
tTime.push(make_pair(truck_weights[i],time_flow));
i++;
}
if( i>= truck_weights.size()){
while(!tTime.empty()){ //pop all, end the situation
time_flow = bridge_length + tTime.front().second;
remain_weight += tTime.front().first;
tTime.pop();
}
}else{ // pop once, see the next situation
time_flow = bridge_length + tTime.front().second-1; // above, plus 1, so in this case, minus 1
remain_weight += tTime.front().first;
tTime.pop();
}
cout<<"reamin_weight < truck_weights, time flow = " <<time_flow<<endl;
}
return time_flow;
}
|