-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path746.cpp
More file actions
70 lines (68 loc) · 1.33 KB
/
Copy path746.cpp
File metadata and controls
70 lines (68 loc) · 1.33 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
#include<iostream>
#include<queue>
using namespace std;
struct cus {
int a, o, l;
bool isLast;
};
int main() {
int n;
while (cin >> n) {
queue<cus> inQueue;
queue<cus> arriving;
for (int i = 0; i < n; i++) {
cus tCus;
cin >> tCus.a >> tCus.o >> tCus.l;
tCus.isLast = (i == n - 1);
arriving.push(tCus);
}
int currentTime = 0;
bool noMoreCus = false;
bool lastCusLeft = true;
cus tCus;
cus lastServe;
while (1) {
if (!inQueue.empty()) {
while (!inQueue.empty() && inQueue.front().o == 0) {
lastServe = inQueue.front();
inQueue.pop();
}
inQueue.front().o--;
while (!inQueue.empty() && inQueue.front().o == 0) {
lastServe = inQueue.front();
inQueue.pop();
}
}
while (arriving.front().a == currentTime) {
tCus = arriving.front();
arriving.pop();
if (inQueue.empty() && tCus.o == 0) {
lastServe = tCus;
}
else if (inQueue.size() <= tCus.l) {
inQueue.push(tCus);
}
if (arriving.empty()) {
noMoreCus = true;
break;
}
}
if (noMoreCus) {
break;
}
currentTime++;
}
while (!inQueue.empty()) {
currentTime += inQueue.front().o;
lastServe = inQueue.front();
inQueue.pop();
}
if (lastServe.isLast) {
cout << currentTime - lastServe.o << endl;
}
else {
cout << -1 << endl;
}
}
return 0;
}