-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceCode.cpp
242 lines (195 loc) · 6.07 KB
/
SourceCode.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Google Hash Code online qualification round 2021
#include <iostream>
#include <queue>
#include <string>
#include <map>
#include <vector>
#include <set>
using namespace std;
// For class-forwarding
class Intersection;
class Street;
class Car;
// Class for each Street
class Street{
public:
string streetName;
int intersectionOut;
int duration; //duration to cross the road
int light_duration = 0;
queue <Car*> queueCars;
Street(string name,int finish,int duration): streetName(name) ,
duration(duration),
intersectionOut(finish){ }
void addCar(Car* car){
this->queueCars.push(car);
}
int getIntersectionOut(){
return this->intersectionOut;
}
};
// Class for each Intersection
class Intersection {
// Public
public:
vector<string> streets;
bool modified = false;
int id;
// Constructor
Intersection(int id): id(id), modified(false) {}
void add_street(string street) {
streets.push_back(street);
}
};
// Class for each Car
class Car{
int id;
queue<string> rues;
public:
// Constructor
Car(int id): id(id) {}
void add_rue(string street) {
rues.push(street);
}
queue<string> get_rues() {
return rues;
}
};
// Function to define how long the traffic lights stay green
void green_light(vector<Car*> cars, vector<Street*> streets, Intersection** intersections) {
// Defining points vector
vector<int> points;
for (int i=0 ; i<streets.size() ; ++i) {
points.push_back(0);
}
// Getting streets based with point system
// Based on each car how many times get passed each street
for (int i=0 ; i<cars.size() ; ++i) {
queue<string> names = cars[i]->get_rues();
int size_queue = names.size();
for (int j=0 ; j<size_queue ; ++j) {
string temp = names.front();
names.pop();
int pos;
for (int k=0 ; k<streets.size() ; ++k) {
if (temp == streets[k]->streetName) {
pos = k;
break;
}
}
++points[pos];
}
}
// Classifier for streets (Bubblesort)
for (int i=0 ; i<streets.size()-1 ; ++i) {
for (int j=i ; j>=0 ; --j) {
if (points[j] < points[j+1]) {
int temp = points[j];
points[j] = points[j+1];
points[j+1] = temp;
Street* t = streets[j];
streets[j] = streets[j+1];
streets[j+1] = t;
}
}
}
// Diving streets into 3 categories
// and giving corresponding light_seconds
int max = points[0];
int min = points[points.size()-1];
int dif = (max-min) / 3;
int mid1 = dif + min;
int mid2 = dif + 2 * min;
for (int i=0 ; i<streets.size() ; ++i) {
if (points[i] < mid1) {
streets[i]->light_duration = 1;
intersections[streets[i]->getIntersectionOut()]->modified = true;
}
else if (points[i] >= mid1 && points[i] < mid2) {
streets[i]->light_duration = 2;
intersections[streets[i]->getIntersectionOut()]->modified = true;
}
else if (points[i] >= mid2) {
streets[i]->light_duration = 3;
intersections[streets[i]->getIntersectionOut()]->modified = true;
}
}
}
// Global map for names -> streets
map <string, Street*> names_to_streets;
int main(){
int D; // Simualtion Duration
int I; // Number of intersections
int S; // number of streets
int V; // the number of cars
int F; // bonus points
cin >> D;
cin >> I;
cin >> S;
cin >> V;
cin >> F;
vector<Street*> streets;
vector<Car*> cars;
Intersection* intersections[I];
// Create Intersections
for(int i = 0; i < I; i++){
intersections[i] = new Intersection(i);
}
// Create Streets
for(int i = 0; i < S; i++){
int B; // Start
int E; // End
cin >> B;
cin >> E;
string street_name;
cin >> street_name;
int L; // Duration
cin >> L;
Street* street = new Street(street_name, E, L);
intersections[E]->add_street(street_name);
streets.push_back(street);
names_to_streets.insert(pair<string, Street*>(street_name, street));
}
// Create cars
for(int i = 0; i < V; i++){
int P; // Number of roads a car goes to
cin >> P;
Car* car = new Car(i);
for(int k = 0; k < P; k++){
string rue;
cin >> rue;
car->add_rue(rue);
if(k==0){ // Starting road of the car
auto itr = names_to_streets.find(rue); // Find the value of the key
Street* street= itr->second;
street->addCar(car); // Add car to street
}
}
cars.push_back(car);
}
// Calculate
green_light(cars, streets, intersections);
int A = 0;
// Find out the number of intersections for which we specify the schedule
for(int i = 0; i < I; i++){
if(intersections[i]->modified)
A++;
}
cout << A << endl;
for(int id = 0; id < I; id++){
if(intersections[id]->modified){
// Print intersection id
cout << id << endl;
vector<string> streets = intersections[id]->streets;
// Print the number of incoming streets (of the intersection id ) covered by this schedule
cout << streets.size() << endl;
for (auto i = streets.begin(); i != streets.end(); ++i){
// The street name
cout << *i << " ";
auto itr = names_to_streets.find(*i);
// How long each street will have a greenlight
cout << itr->second->light_duration << endl;
}
}
}
}