-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path746.cpp
More file actions
131 lines (122 loc) · 2.15 KB
/
746.cpp
File metadata and controls
131 lines (122 loc) · 2.15 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*****************************************
* (This comment block is added by the Judge System)
* Submission ID: 67351
* Submitted at: 2018-10-19 00:07:51
*
* User ID: 539
* Username: 55211931
* Problem ID: 746
* Problem Name: Fast Food Restaurant
*/
#include <iostream>
using namespace std;
class MyQueue
{
private:
int front=0;
int rear=0;
int finishTime[200000];
int TOTAL_SLOTS = 200000;
public:
bool isEmpty();
bool isFull();
void enqueue(int a);
int dequeue();
int getSlots();
int getFront();
};
int MyQueue::getSlots()
{
return (rear - front);
}
bool MyQueue::isEmpty()
{
return (front == rear);
}
bool MyQueue::isFull()
{
return((rear + 1) % TOTAL_SLOTS == front);
}
void MyQueue::enqueue(int data)
{
if (!isFull())
{
finishTime[rear] = data;
rear = (rear + 1);
}
}
int MyQueue::dequeue()
{
int ret_val;
if (!isEmpty())
{
ret_val = finishTime[front];
front = (front + 1) % TOTAL_SLOTS;
return ret_val;
}
}
int MyQueue::getFront()
{
return finishTime[front];
}
int main()
{
int testTimes;
while (cin >> testTimes)
{
bool state = true;
MyQueue q;
int nextFinish;
int startTime, runTime, waitNo;
cin >> startTime >> runTime >> waitNo;
nextFinish = startTime + runTime;
q.enqueue(nextFinish);
for (int i = 0; i < testTimes-1;i++)
{
cin >> startTime >> runTime >> waitNo;
while (q.getFront() <= startTime&&!q.isEmpty())
{
q.dequeue();
}
if (waitNo >= q.getSlots())
{
state = true;
if (q.isEmpty())
{
q.enqueue(startTime + runTime);
nextFinish = (startTime + runTime);
}
else
{
if (startTime >= nextFinish)
{
while (!q.isEmpty())
{
q.dequeue();
}
nextFinish = (startTime + runTime);
q.enqueue(nextFinish);
}
else
{
nextFinish += runTime;
q.enqueue(nextFinish);
}
}
}
else
{
state = false;
}
}
if (state == true)
{
cout << (nextFinish - runTime) << endl;
}
else
{
cout << "-1" << endl;
}
}
return 0;
}