-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
215 lines (174 loc) · 6.7 KB
/
main.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
#include <queue>
int countPC, occupiedPC = 0, priceH, timeStart, timeEnd;
std::map<int, std::pair<std::string, std::string>> tablesForward; // стол - (время, имя)
std::map<std::string, int> tablesBack; // имя - стол
std::set<std::string> camePeople;
std::queue<std::string> quOnTables;
std::map<int, std::pair<int, int>> score; // комп - (счет, общее время)
int TimeToInt(std::string time){
int ans = 0;
ans += ((time[0] - '0') * 10 + (time[1] - '0'))*60 + (time[3] - '0') * 10 + (time[4] - '0');
return ans;
}
std::string TimeToStr(int time){
std::string h, m;
h = std::to_string(time/60); m = std::to_string(time%60);
while (h.length() < 2)
h = "0"+h;
while (m.length() < 2)
m = "0"+m;
return h+':'+m;
}
class OutgoingEvents{
protected:
void id11(std::string time, std::string name){
// std::cout << time << " 11 " << name << std::endl;
camePeople.erase(camePeople.find(name));
if (tablesBack[name] != -1){
int diffTime = TimeToInt(time)-TimeToInt(tablesForward[tablesBack[name]].first);
int cash = (diffTime+59)/60*priceH;
score[tablesBack[name]].first += cash;
score[tablesBack[name]].second += diffTime;
tablesForward[tablesBack[name]].first = "";
tablesForward[tablesBack[name]].second = "";
occupiedPC--;
}
}
void id12(std::string time, std::string name){
if (tablesBack[name] != -1){
if (!quOnTables.empty()){
std::string newPer = quOnTables.front();
quOnTables.pop();
id11(time, name);
occupiedPC++; // А то в id 11 вы его вычитаем
tablesForward[tablesBack[name]].first = time;
tablesForward[tablesBack[name]].second = newPer;
tablesBack[newPer] = tablesBack[name];
tablesBack.erase(tablesBack.find(name));
std::cout << time << " 12 " << newPer << ' ' << tablesBack[newPer]+1 << std::endl;
}
else{
id11(time, name);
tablesForward[tablesBack[name]].second = "";
tablesBack.erase(tablesBack.find(name));
}
}
}
std::string id13(std::string time, std::string error){
return time + " 13 " + error;
}
};
class IncomingEvents: protected OutgoingEvents{
protected:
void id1(std::string time, std::string name){
std::cout << time << " 1 " << name << std::endl;
int coming = TimeToInt(time);
if (coming < timeStart || coming > timeEnd)
std::cout << id13(time, "NotOpenYet") << std::endl;
else if (camePeople.find(name) != camePeople.end())
std::cout << id13(time, "YouShallNotPass") << std::endl;
else{
camePeople.insert(name);
tablesBack[name] = -1;
}
}
void id2(std::string time, std::string name, int numT){
std::cout << time << " 2 " << name << ' ' << numT << std::endl;
numT--;
if (camePeople.find(name) == camePeople.end())
std::cout << id13(time, "ClientUnknown") << std::endl;
else if (tablesForward[numT].second != "")
std::cout << id13(time, "PlaceIsBusy") << std::endl;
else{
if (tablesBack[name] != -1){
tablesForward[tablesBack[name]].second = "";
occupiedPC--;
}
tablesForward[numT].first = time;
tablesForward[numT].second = name;
tablesBack[name] = numT;
occupiedPC++;
}
}
void id3(std::string time, std::string name){
std::cout << time << " 3 " << name << std::endl;
if (countPC > occupiedPC)
std::cout << id13(time, "ICanWaitNoLonger!") << std::endl;
else if (countPC <= quOnTables.size()){
std::cout << time << " 11 " << name << std::endl;
id11(time, name);
tablesBack.erase(tablesBack.find(name));
}
else{
quOnTables.push(name);
}
}
void id4(std::string time, std::string name){
std::cout << time << " 4 " << name << std::endl;
if (camePeople.find(name) == camePeople.end())
std::cout << id13(time, "ClientUnknown") << std::endl;
else{
id12(time, name);
}
}
};
class Requests: protected IncomingEvents{
private:
std::string time;
int id;
std::string person;
int numT;
public:
Requests(std::string valueTime, int numId, std::string namePerson, int numTable=-1){
time = valueTime;
id = numId;
person = namePerson;
numT = numTable;
}
void event(){
if (id == 1)
id1(time, person);
else if (id == 2)
id2(time, person, numT);
else if (id == 3)
id3(time, person);
else if (id == 4)
id4(time, person);
else if (id == 11)
id11(time, person);
}
};
int main(int argc, char* argv[]){
freopen(argv[1], "r", stdin);
std::string strTimeStart, strTimeEnd;
std::cin >> countPC >> strTimeStart >> strTimeEnd >> priceH;
timeStart = TimeToInt(strTimeStart); timeEnd = TimeToInt(strTimeEnd);
for (int i = 0; i < countPC; i++){
tablesForward[i].second = "";
}
std::cout << strTimeStart << std::endl;
std::string time, name;
int id, numT;
while (std::cin >> time >> id >> name){ // Читаем файлик
if (id == 2)
std::cin >> numT;
Requests request(time, id, name, numT);
request.event();
}
// Закрытие клуба
while (camePeople.size() > 0){
std::string name = *camePeople.begin();
std::cout << strTimeEnd + " 11 " + name << std::endl;
Requests request(strTimeEnd, 11, name);
request.event();
}
// Финальный счет
std::cout << strTimeEnd << std::endl;
for (auto elem : score)
std::cout << elem.first+1 << ' ' << elem.second.first << ' ' << TimeToStr(elem.second.second) << std::endl;
}