forked from fatemehkarimi/uvaSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva-10901.cpp
More file actions
115 lines (94 loc) · 2.14 KB
/
uva-10901.cpp
File metadata and controls
115 lines (94 loc) · 2.14 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//uva 10901
//Ferry Loading III
#include <iostream>
#include <climits>
#include <string>
#include <queue>
#include <map>
using namespace std;
int main(void)
{
int T = 0;
cin >> T;
while (T--) {
int n, t, m;
cin >> n >> t >> m;
queue < pair <int, int> > left;
queue < pair <int, int> > right;
for (int i = 0; i < m; ++i){
int tmp;
string pos;
cin >> tmp >> pos;
if (pos == "left")
left.push(make_pair(tmp, i));//tmp = departure time, i = car code
else
right.push(make_pair(tmp, i));
}
map <int, int> result;//key = car code, value = arrival time
int curr_time = 0;
bool position = 0;//0 indicates left, 1 indicates right
while (!left.empty() || !right.empty()) {
bool ferryEmpty = 1;
if (position){
int counter = 0;
while (!right.empty() && counter < n && right.front().first <= curr_time){
pair <int, int> tmp = right.front();
right.pop();
result[tmp.second] = curr_time + t;
ferryEmpty = 0;
++counter;
}
}
else {
int counter = 0;
while (!left.empty() && counter < n && left.front().first <= curr_time){
pair <int, int> tmp = left.front();
left.pop();
result[tmp.second] = curr_time + t;
ferryEmpty = 0;
++counter;
}
}
if (!ferryEmpty) {
curr_time += t;
position = !position;
}
else {
pair <int, int> carR, carL;
if (!right.empty())
carR = right.front();
else
carR = make_pair(INT_MAX, INT_MAX);
if (!left.empty())
carL = left.front();
else
carL = make_pair(INT_MAX, INT_MAX);
if (carR.first < carL.first){
if (position)
curr_time = max(curr_time, carR.first);
else {
curr_time = max(curr_time, carR.first);
curr_time += t;
position = 1;
}
}
else if (carR.first > carL.first){
if (!position)
curr_time = max(curr_time, carL.first);
else{
curr_time = max(curr_time, carL.first);
curr_time += t;
position = 0;
}
}
else
curr_time = max(curr_time, carR.first);
}
}
for (auto a : result)
cout << a.second << endl;
if (T != 0)
cout << endl;
}
return 0;
}