forked from ajahuang/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa 11034 - Ferry Loading IV.cpp
51 lines (47 loc) · 1.15 KB
/
UVa 11034 - Ferry Loading IV.cpp
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
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
#include <queue>
#include <string>
using namespace std;
static const int LEFT = 0;
static const int RIGHT = 1;
int main()
{
int c;
cin >> c;
while ( c-- )
{
int l, m;
cin >> l >> m;
l *= 100;
// Use two queues to store the cars.
vector<queue<int> > cars(2);
for (int i = 0; i < m; ++i)
{
int length;
string bank;
cin >> length >> bank;
if (bank == "left")
cars[LEFT].push(length);
else
cars[RIGHT].push(length);
}
// "The ferry is initially on the left bank"
int index = LEFT;
int crossRiver = 0;
while (! (cars[LEFT].empty() && cars[RIGHT].empty()))
{
int totalLength = 0;
while (! cars[index].empty()
&& totalLength + cars[index].front() <= l)
{
totalLength += cars[index].front();
cars[index].pop();
}
++crossRiver;
index ^= 1;
}
cout << crossRiver << endl;
}
return 0;
}