some dp problems
This commit is contained in:
123
CSES - CSES Problem Set/Sorting_And_Searching/Traffic_Lights.cpp
Normal file
123
CSES - CSES Problem Set/Sorting_And_Searching/Traffic_Lights.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
//
|
||||
// Created by darian on 07.06.24.
|
||||
//
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
//MISSING distances[p.second - p.first]--; !!!!!!
|
||||
//int main() {
|
||||
// ll x; cin >> x;
|
||||
// int n; cin >> n;
|
||||
// map<ll, int> distances;
|
||||
// distances[x] = 1;
|
||||
// set<pair<ll, ll>> 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;
|
||||
|
||||
set<ll> lights{0, x};
|
||||
map<ll, int> dist{{x, 1}};
|
||||
|
||||
for (int i{}; i < n; i++) {
|
||||
ll a; cin >> a;
|
||||
auto it = lights.upper_bound(a);
|
||||
auto it2 = it;
|
||||
it2--;
|
||||
|
||||
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 <algorithm>
|
||||
//#include <iostream>
|
||||
//#include <vector>
|
||||
//
|
||||
//using std::cout;
|
||||
//using std::endl;
|
||||
//using std::vector;
|
||||
//
|
||||
//int main() {
|
||||
// int street_len;
|
||||
// int light_num;
|
||||
// std::cin >> street_len >> light_num;
|
||||
// vector<int> lights(light_num);
|
||||
// for (int &l : lights) { std::cin >> l; }
|
||||
//
|
||||
// vector<std::pair<int, int>> 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<int> 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> 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<int> 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;
|
||||
//}
|
||||
Reference in New Issue
Block a user