1) The cars are loaded in the order of their arrival;
2) The ferry is initially on the left bank;
2) The ferry is initially on the left bank;
*3) If you get the error "deque iterator not dereferenceable" , make sure that you are not calling pop()/front() on empty queue.
Here's the code:
- #include<iostream>
- #include<queue>
- #include<string>
- using namespace std;
- int main()
- {
- int laps , l, m, c, car, load;
- string bank;
- bool rightBank;
- cin >> c;
- for (int i = 0; i < c; i++){
- cin >> l >> m;
- queue<int>Left, Right;
- load = 0;
- rightBank = 0;
- for (int j = 0; j < m; j++){
- cin >> car >> bank;
- if (bank == "left")
- Left.push(car);
- else
- Right.push(car);
- }
- laps = 0; l *= 100;
- while (!Left.empty() || !Right.empty()){
- load = 0;
- if (rightBank){
- while (!Right.empty() && load + Right.front() <= l)
- {
- load += Right.front(); Right.pop();
- }
- }
- else{
- while (!Left.empty() && load + Left.front() <= l)
- {
- load += Left.front(); Left.pop();
- }
- }
- ++laps;
- rightBank = 1 - rightBank;
- }
- cout << laps << endl;
- }
- return 0;
- }
Sample input:
9
1 4
100 right
100 right
100 right
100 right
1 3
20 left
81 left
99 left
1 1
100 right
15 4
380 right
720 left
1340 right
1040 left
Sample output:
6
5
2
4
in the first test case shouldn't it be 8?
ReplyDelete