classUndergroundSystem { unordered_map<int, pair<string, int>> user_checkin_time; // 记录进站信息 map<pair<string, string>, pair<int, int>> interval_total_time_person_count; // 记录2站之间流量的信息,总时间、人数 public: UndergroundSystem() { } voidcheckIn(int id, string stationName, int t){ user_checkin_time[id] = {stationName, t}; } voidcheckOut(int id, string stationName, int t){ auto in = user_checkin_time[id]; auto& interval = interval_total_time_person_count[{in.first, stationName}]; interval.first += t - in.second; ++interval.second; user_checkin_time.erase(id); } doublegetAverageTime(string startStation, string endStation){ auto it = interval_total_time_person_count.find({startStation, endStation}); if (it == interval_total_time_person_count.end()) return-1; else returnstatic_cast<double>(it->second.first) / it->second.second; } };
/** * Your UndergroundSystem object will be instantiated and called as such: * UndergroundSystem* obj = new UndergroundSystem(); * obj->checkIn(id,stationName,t); * obj->checkOut(id,stationName,t); * double param_3 = obj->getAverageTime(startStation,endStation); */