#include #include #include #include #include using namespace std; struct GUARD{ int id; vector v;//Cycle int index; int start_wait; int GetTime()const{ return v[index % v.size()]; } GUARD() : id(-1), index(0), start_wait(0){} }; enum TYPE{ END, LEAVE, ENTER, START_CONSUME, START_CHARGE }; const string STR[] = {"END","START_CHARGE","START_CONSUME","ENTER","LEAVE"}; struct EVENT { int type; int time; int id; bool operator<(const EVENT& rh)const{ if (time == rh.time){ return id < rh.id; } return time < rh.time; } EVENT(int type, int id, int time){ this->type = type; this->time = time; this->id = id; } void Print()const { cout << "EVENT type:" << STR[type] << " time:" << time << " id:" << id << endl; } }; void Print(const string& s, int id, int time) { // cout << s << " time:" << time << " id:" << id << endl; } main() { int guard, duration; while (cin >> guard >> duration && !(guard == 0 && duration == 0)){ int answer = 0; vector guards; set events; events.insert(EVENT(END, -1, duration)); for (int i = 0; i < guard; ++i){ GUARD g; g.id = i; int con; while (cin >> con && con != 0){ int cha; cin >> cha; g.v.push_back(con); g.v.push_back(cha); } g.start_wait = 0; guards.push_back(g); events.insert(EVENT(START_CONSUME, i, 0)); } cout << events.begin()->time << " " << events.size() << endl; for (set::iterator it = events.begin(); it != events.end(); ++it){ it->Print(); } deque q; while (events.begin()->type != END){ const EVENT e = *events.begin(); events.erase(events.begin()); GUARD& g = guards[e.id]; if (e.type == START_CONSUME){ Print("START_CONSUME", e.id, e.time); int t = e.time + g.GetTime(); ++g.index; events.insert(EVENT(ENTER, e.id, t)); } else if (e.type == ENTER){ Print("ENTER ", e.id, e.time); g.start_wait = e.time; q.push_back(e.id); if (q.size() == 1){ events.insert(EVENT(START_CHARGE, e.id, e.time)); } } else if (e.type == START_CHARGE){ Print("START_CHARGE ", e.id, e.time); int t = e.time + g.GetTime(); ++g.index; events.insert(EVENT(START_CONSUME, e.id, t)); answer += e.time - g.start_wait; q.pop_front(); if (!q.empty()){ events.insert(EVENT(START_CHARGE, q.front(), t)); } } } for (deque::iterator it = q.begin(); it != q.end(); ++it){ answer += duration - guards[*it].start_wait; } cout << answer << endl; } }