#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(signed N, std::vector<signed> 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]]]);
}
}
signed max_towers(signed L, signed R, signed 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_}; };
cerr << sz(p) << '\n';
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)));
//cerr << v[p[i]] << ' ' << v[p[urm]] << '\n';
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.insert(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.insert(get(urm));
}
}
return sz(heap) + 1;
}
#undef int
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4025 ms |
3180 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Runtime error |
2 ms |
4696 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Runtime error |
2 ms |
4696 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4013 ms |
6116 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
16 ms |
6488 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Runtime error |
2 ms |
4696 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4025 ms |
3180 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |