# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1101838 | blackslex | Global Warming (NOI13_gw) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
int gw (int N, int *H) {
int n = N;
vector<int> h(n), par(n); iota(par.begin(), par.end(), 0);
vector<bool> ck(n);
for (int i = 0; i < n; i++) h[i] = H[i];
int cnt = 0, ans = 0;
function<int(int)> fset = [&] (int x) {return (par[x] == x ? x : par[x] = fset(par[x]));};
auto mg = [&] (int x, int y) {
if ((x = fset(x)) == (y = fset(y))) return;
if (ck[x] || ck[y]) par[y] = x, ck[x] = ck[y] = 1;
else {
cnt++;
par[y] = x; ck[x] = ck[y] = 1;
}
};
vector<int> c = h;
sort(c.begin(), c.end()); c.resize(unique(c.begin(), c.end()) - c.begin());
vector<vector<int>> add(n, vector<int>());
auto get = [&] (int x) {
return lower_bound(c.begin(), c.end(), x) - c.begin();
};
for (int i = 0; i < n; i++) add[get(h[i])].emplace_back(i);
for (int i = n - 1; i >= 0; i--) {
for (auto &e: add[i]) {
if (e && h[e - 1] >= h[e]) mg(e - 1, e);
if (e != n - 1 && h[e + 1] >= h[e]) mg(e, e + 1);
}
ans = max(ans, cnt);
}
return ans;
}