#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;
set<int> indemnizatii;
for(int i = 0; i < sz(p) - 1; i++) {
indemnizatii.emplace(i);
indemnizatii.emplace(i + 1);
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 << sz(heap) << ": " << C << ' ' << i << '\n';
i = DSU::f(i);
int urm = DSU::nxt[i];
if(i != *indemnizatii.begin()) {
int ant = DSU::f(i - 1);
heap.erase(heap.find(get(ant)));
}
if(urm != *indemnizatii.rbegin())
heap.erase(heap.find(get(urm)));
//cerr << '\t' << v[p[i]] << ' ' << v[p[urm]] << '\n';
if(v[p[i]] < v[p[urm]]) {
DSU::unite(i, urm);
indemnizatii.erase(urm);
if(i != *indemnizatii.begin()) {
int ant = DSU::f(i - 1);
heap.insert(get(ant));
}
if(i != *indemnizatii.rbegin())
heap.insert(get(i));
}
else {
if(i != *indemnizatii.begin()) {
int ant = DSU::f(i - 1);
DSU::unite(ant, i);
heap.insert(get(ant));
}
if(urm != *indemnizatii.rbegin())
heap.insert(get(urm));
indemnizatii.erase(i);
}
}
return sz(heap) + 1;
}
#undef int
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1406 ms |
3180 KB |
Output is correct |
2 |
Correct |
3701 ms |
3536 KB |
Output is correct |
3 |
Correct |
3409 ms |
3536 KB |
Output is correct |
4 |
Correct |
3226 ms |
3536 KB |
Output is correct |
5 |
Execution timed out |
4016 ms |
3536 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
0 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2636 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
1 ms |
2648 KB |
Output is correct |
31 |
Correct |
1 ms |
2392 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2392 KB |
Output is correct |
34 |
Correct |
0 ms |
2392 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
0 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2636 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
1 ms |
2648 KB |
Output is correct |
31 |
Correct |
1 ms |
2392 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2392 KB |
Output is correct |
34 |
Correct |
0 ms |
2392 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
36 |
Correct |
8 ms |
3928 KB |
Output is correct |
37 |
Correct |
22 ms |
5060 KB |
Output is correct |
38 |
Correct |
11 ms |
4048 KB |
Output is correct |
39 |
Correct |
30 ms |
5584 KB |
Output is correct |
40 |
Correct |
12 ms |
4304 KB |
Output is correct |
41 |
Correct |
56 ms |
8560 KB |
Output is correct |
42 |
Correct |
11 ms |
3792 KB |
Output is correct |
43 |
Correct |
8 ms |
3536 KB |
Output is correct |
44 |
Correct |
8 ms |
3536 KB |
Output is correct |
45 |
Correct |
14 ms |
3536 KB |
Output is correct |
46 |
Correct |
15 ms |
3520 KB |
Output is correct |
47 |
Correct |
14 ms |
5072 KB |
Output is correct |
48 |
Correct |
13 ms |
4816 KB |
Output is correct |
49 |
Correct |
10 ms |
4048 KB |
Output is correct |
50 |
Correct |
8 ms |
3512 KB |
Output is correct |
51 |
Correct |
8 ms |
3536 KB |
Output is correct |
52 |
Correct |
33 ms |
7612 KB |
Output is correct |
53 |
Correct |
37 ms |
9556 KB |
Output is correct |
54 |
Correct |
35 ms |
9596 KB |
Output is correct |
55 |
Correct |
9 ms |
3536 KB |
Output is correct |
56 |
Correct |
9 ms |
3536 KB |
Output is correct |
57 |
Correct |
46 ms |
7580 KB |
Output is correct |
58 |
Correct |
45 ms |
7632 KB |
Output is correct |
59 |
Correct |
36 ms |
7632 KB |
Output is correct |
60 |
Correct |
35 ms |
9680 KB |
Output is correct |
61 |
Correct |
46 ms |
9672 KB |
Output is correct |
62 |
Correct |
65 ms |
9672 KB |
Output is correct |
63 |
Correct |
45 ms |
9672 KB |
Output is correct |
64 |
Correct |
8 ms |
3536 KB |
Output is correct |
65 |
Correct |
14 ms |
3624 KB |
Output is correct |
66 |
Correct |
11 ms |
3536 KB |
Output is correct |
67 |
Correct |
15 ms |
3500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4091 ms |
7632 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4067 ms |
3672 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
0 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2636 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
1 ms |
2648 KB |
Output is correct |
31 |
Correct |
1 ms |
2392 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2392 KB |
Output is correct |
34 |
Correct |
0 ms |
2392 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
36 |
Correct |
8 ms |
3928 KB |
Output is correct |
37 |
Correct |
22 ms |
5060 KB |
Output is correct |
38 |
Correct |
11 ms |
4048 KB |
Output is correct |
39 |
Correct |
30 ms |
5584 KB |
Output is correct |
40 |
Correct |
12 ms |
4304 KB |
Output is correct |
41 |
Correct |
56 ms |
8560 KB |
Output is correct |
42 |
Correct |
11 ms |
3792 KB |
Output is correct |
43 |
Correct |
8 ms |
3536 KB |
Output is correct |
44 |
Correct |
8 ms |
3536 KB |
Output is correct |
45 |
Correct |
14 ms |
3536 KB |
Output is correct |
46 |
Correct |
15 ms |
3520 KB |
Output is correct |
47 |
Correct |
14 ms |
5072 KB |
Output is correct |
48 |
Correct |
13 ms |
4816 KB |
Output is correct |
49 |
Correct |
10 ms |
4048 KB |
Output is correct |
50 |
Correct |
8 ms |
3512 KB |
Output is correct |
51 |
Correct |
8 ms |
3536 KB |
Output is correct |
52 |
Correct |
33 ms |
7612 KB |
Output is correct |
53 |
Correct |
37 ms |
9556 KB |
Output is correct |
54 |
Correct |
35 ms |
9596 KB |
Output is correct |
55 |
Correct |
9 ms |
3536 KB |
Output is correct |
56 |
Correct |
9 ms |
3536 KB |
Output is correct |
57 |
Correct |
46 ms |
7580 KB |
Output is correct |
58 |
Correct |
45 ms |
7632 KB |
Output is correct |
59 |
Correct |
36 ms |
7632 KB |
Output is correct |
60 |
Correct |
35 ms |
9680 KB |
Output is correct |
61 |
Correct |
46 ms |
9672 KB |
Output is correct |
62 |
Correct |
65 ms |
9672 KB |
Output is correct |
63 |
Correct |
45 ms |
9672 KB |
Output is correct |
64 |
Correct |
8 ms |
3536 KB |
Output is correct |
65 |
Correct |
14 ms |
3624 KB |
Output is correct |
66 |
Correct |
11 ms |
3536 KB |
Output is correct |
67 |
Correct |
15 ms |
3500 KB |
Output is correct |
68 |
Execution timed out |
4091 ms |
7632 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1406 ms |
3180 KB |
Output is correct |
2 |
Correct |
3701 ms |
3536 KB |
Output is correct |
3 |
Correct |
3409 ms |
3536 KB |
Output is correct |
4 |
Correct |
3226 ms |
3536 KB |
Output is correct |
5 |
Execution timed out |
4016 ms |
3536 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |