#include "towers.h"
#include <vector>
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 1e5 + 5;
int v[nmax];
void init(int N, std::vector<int> H) {
for(int i = 0; i < N; i++) v[i + 1] = H[i];
}
namespace DSU {
int dsu[nmax], nxt[nmax];
int mxR[nmax];
void init(int l, int r) {
for(int i = l; i <= r; i++) dsu[i] = i, nxt[i] = i + 1;
return;
}
int f(int x) { return x == dsu[x]? x : dsu[x] = f(dsu[x]); }
void unite(int x, int y) {
x = f(x);
y = f(y);
if(x == y) return;
if(x > y) swap(x, y);
dsu[y] = x;
mxR[x] = max(mxR[x], mxR[y]);
nxt[x] = nxt[y];
return;
}
int coef(int i, const vector<int>& p) {
i = f(i);
return mxR[i] - max(v[p[i]], v[p[nxt[i]]]);
}
}
int max_towers(int L, int R, int D) {
++L;
++R;
vector<int> p;
for(int i = L; i <= R; i++) {
if((i == L || v[i] < v[i - 1]) && (i == R || v[i] < v[i + 1])) p.emplace_back(i);
}
DSU::init(0, sz(p));
set<pii> heap;
for(int i = 0; i < sz(p) - 1; i++) {
int mx = v[p[i] + 1];
for(int j = p[i] + 2; j < p[i + 1]; j++)
mx = max(mx, v[j]);
DSU::mxR[i] = mx;
heap.emplace(mx - max(v[p[i]], v[p[i + 1]]), i);
}
auto get = [&](int p_) { return pii{DSU::coef(p_, p), p_}; };
while(!heap.empty() && heap.begin() -> first < D) {
auto [C, i] = *heap.begin();
heap.erase(heap.begin());
cerr << C << ' ' << i << '\n';
i = DSU::f(i);
int urm = DSU::nxt[i];
if(i != 0) {
int ant = DSU::f(i - 1);
heap.erase(heap.find(get(ant)));
}
if(urm != sz(p) - 1)
heap.erase(heap.find(get(urm)));
if(v[p[i]] < v[p[urm]]) {
DSU::unite(i, urm);
if(i != 0) {
int ant = DSU::f(i - 1);
heap.insert(get(ant));
}
if(DSU::nxt[i] != sz(p) - 1)
heap.erase(heap.find(get(i)));
}
else {
if(i != 0) {
int ant = DSU::f(i - 1);
DSU::unite(ant, i);
heap.insert(get(ant));
}
if(urm != sz(p) - 1)
heap.erase(heap.find(get(urm)));
}
}
return sz(heap) + 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4070 ms |
1112 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4074 ms |
3320 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
1876 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4070 ms |
1112 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |