#include #include #include #include #include using namespace std; struct Event { int time_; int type; //0 - пришел человек, 1 - парикмахер освободился int manNum; int parNum; Event() {} Event(int time_, int type, int manNum, int parNum) :time_(time_), type(type), manNum(manNum), parNum(parNum) {} bool operator <(const Event& other) { return time_ < other.time_ || (time_ == other.time_ && type > other.type); } }; int main() { set ev; int N; cin >> N; for (int i = 0; i < N; ++i) { int h, m; cin >> h >> m; ev.insert(Event(h * 60 + m, 0, i, -1)); } queue q; vector freePar(3, true); vector ans(N); while (!ev.empty()) { Event cur = *ev.begin(); ev.erase(ev.begin()); if (cur.type == 0) { bool good = false; for (int i = 0; i < 3; ++i) { if (freePar[i]) { ev.insert(Event(cur.time_ + 30, 1, cur.manNum, i)); good = true; freePar[i] = false; break; } } if (!good) { q.push(cur.manNum); } } else { ans[cur.manNum] = cur.time_; if (q.empty()) { freePar[cur.parNum] = true; } else { int curMan = q.front(); q.pop(); ev.insert(Event(cur.time_ + 30, 1, curMan, cur.parNum)); } } } }