-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1396.cpp
More file actions
63 lines (53 loc) · 2 KB
/
1396.cpp
File metadata and controls
63 lines (53 loc) · 2 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
// Problem : 1396.
// Link :
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class UndergroundSystem {
private:
unordered_map<int, pair<int, string>> movingCustomers;
unordered_map<string , unordered_map<string, pair<int,int>>> averageTime;
public:
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
movingCustomers[id] = make_pair(t, stationName);
}
void checkOut(int id, string stationName, int t) {
string lastStation = movingCustomers[id].second;
int startTime = movingCustomers[id].first;
int lastAvg = averageTime[lastStation][stationName].first;
int avgTime = 0;
if (lastAvg != 0)
avgTime = ((t - startTime) + lastAvg) / averageTime[lastStation][stationName].second + 1;
else
avgTime = ((t - startTime)) / 1;
if (lastStation.at(0) < stationName.at(0))
averageTime[lastStation].insert(make_pair(stationName, make_pair(avgTime,averageTime[lastStation][stationName].second + 1)));
else
averageTime[stationName].insert(make_pair(lastStation, make_pair(avgTime,averageTime[lastStation][stationName].second + 1)));
movingCustomers.erase(id);
}
double getAverageTime(string startStation, string endStation) {
if (startStation.at(0) < endStation.at(0))
return averageTime[startStation][endStation].first;
else
return averageTime[endStation][startStation].first;
}
};
/**
* 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);
*/
int main() {
UndergroundSystem* obj = new UndergroundSystem();
obj->checkIn(45,"Leyton",3);
obj->checkOut(45,"Waterloo",15);
double param_3 = obj->getAverageTime("Leyton","Waterloo");
cout << param_3;
return 0;
}