From 66d059f2300293b89485a3368b6f902d9e92fbe2 Mon Sep 17 00:00:00 2001 From: DarianTr Date: Sun, 9 Jun 2024 15:25:05 +0200 Subject: [PATCH] traffic lights works --- CSES - CSES Problem Set/Traffic_Lights.cpp | 125 ++++++++++++++++++--- 1 file changed, 109 insertions(+), 16 deletions(-) diff --git a/CSES - CSES Problem Set/Traffic_Lights.cpp b/CSES - CSES Problem Set/Traffic_Lights.cpp index bba7b35..25540b5 100644 --- a/CSES - CSES Problem Set/Traffic_Lights.cpp +++ b/CSES - CSES Problem Set/Traffic_Lights.cpp @@ -6,25 +6,118 @@ using namespace std; using ll = long long; +//MISSING distances[p.second - p.first]--; !!!!!! +//int main() { +// ll x; cin >> x; +// int n; cin >> n; +// map distances; +// distances[x] = 1; +// set> lines = {{0, x}}; +// +// for (int i{}; i < n; i++) { +// ll t; cin >> t; +// auto it = lines.lower_bound({t, t}); +// --it; +// auto p = *it; +// lines.erase(p); +// lines.insert({p.first, t}); +// lines.insert({t, p.second}); +// distances[p.second - p.first]--; +// if (distances[p.second - p.first] == 0) distances.erase(p.second - p.first); +// distances[t - p.first]++; +// distances[p.second - t]++; +// cout << (*distances.rbegin()).first << endl; +// } +//} +// + +//slightly inspirated from usaco int main() { ll x; cin >> x; int n; cin >> n; - map distances; - distances[x] = 1; - set> lines = {{0, x}}; + + set lights{0, x}; + map dist{{x, 1}}; for (int i{}; i < n; i++) { - ll t; cin >> t; - auto it = lines.lower_bound({t, t}); - --it; - auto p = *it; - lines.erase(p); - lines.insert({p.first, t}); - lines.insert({t, p.second}); - if (distances[p.second - p.first] == 1) distances.erase(p.second - p.first); - distances[t - p.first]++; - distances[p.second - t]++; - cout << (*distances.rbegin()).first << endl; - } + ll a; cin >> a; + auto it = lights.upper_bound(a); + auto it2 = it; + it2--; -} \ No newline at end of file + dist[a - *it2]++; + dist[*it - a]++; + dist[*it - *it2]--; + if (!dist[*it - *it2]) { + dist.erase(*it - *it2); + } + lights.insert(a); + cout << (*dist.rbegin()).first << endl; + } +} + + +//faster solution from usaco +//#include +//#include +//#include +// +//using std::cout; +//using std::endl; +//using std::vector; +// +//int main() { +// int street_len; +// int light_num; +// std::cin >> street_len >> light_num; +// vector lights(light_num); +// for (int &l : lights) { std::cin >> l; } +// +// vector> sorted_lights(light_num); +// for (int l = 0; l < light_num; l++) { sorted_lights[l] = {lights[l], l}; } +// std::sort(sorted_lights.begin(), sorted_lights.end()); +// +// // Given the light position, this array stores its position in +// // sorted_lights. +// vector new_pos(light_num); +// for (int l = 0; l < light_num; l++) { +// new_pos[sorted_lights[l].second] = l; +// } +// +// struct Light { +// int prev, next; +// int pos; +// }; +// vector light_ll(light_num + 2); +// // First, we set our "lights" on the edges of the street. +// light_ll[0] = {-1, 1, 0}; +// light_ll[light_num + 1] = {light_num, -1, street_len}; +// for (int l = 0; l < light_num; l++) { +// light_ll[l + 1] = {l, l + 2, sorted_lights[l].first}; +// } +// +// // Find the longest passage once all the streetlights are added +// vector gaps(light_num); +// int max_gap = 0; +// for (int l = 0; l <= light_num; l++) { +// max_gap = std::max(max_gap, light_ll[l + 1].pos - light_ll[l].pos); +// } +// gaps.back() = max_gap; +// +// // Remove the streetlights in reverse order like as we did in the above +// // solution. +// for (int l = light_num - 1; l > 0; l--) { +// Light to_del = light_ll[new_pos[l] + 1]; +// Light &left = light_ll[to_del.prev]; +// Light &right = light_ll[to_del.next]; +// // Re-assign the references to the next & previous node +// left.next = to_del.next; +// right.prev = to_del.prev; +// +// max_gap = std::max(max_gap, right.pos - left.pos); +// gaps[l - 1] = max_gap; +// } +// +// for (int i = 0; i < gaps.size() - 1; i++) { cout << gaps[i] << ' '; } +// cout << gaps.back() << endl; +//} \ No newline at end of file