#include "towers.h"
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using arr = array<int, 2>;
using arrr = array<int, 3>;
constexpr int maxn = 1e5 + 5;
int n;
int h[maxn], ledge[maxn] = { 0 }, redge[maxn] = { 0 }, owner[maxn] = { 0 };
bool removed[maxn] = { 0 };
int out = 0;
void uni(int a, int b)
{
assert(a != b);
out--;
h[a] = min(h[a], h[b]);
ledge[a] = min(ledge[a], ledge[b]);
redge[a] = max(redge[a], redge[b]);
owner[ledge[a] + 1] = owner[redge[a] - 1] = a;
removed[b] = true;
}
vector <arr> output; // {d val, output}
void init(int N, std::vector<int> H) {
n = N;
for (int i = 0; i < n; i++) h[i + 1] = H[i];
h[0] = h[n + 1] = 2e9;
vector <arrr> edges; // {height diff, from, to}
edges.reserve(n - 1);
vector <int> minima;
for (int i = 1; i <= n; i++) {
if (h[i - 1] > h[i] && h[i] < h[i + 1]) {
minima.emplace_back(i);
}
}
priority_queue <arr, vector<arr>, greater<arr>> pq;
for (int i : minima)
{
ledge[i] = i - 1, redge[i] = i + 1;
owner[i] = i;
if (i != 1) pq.push(arr{h[ledge[i]] - h[i], i});
if (i != n) pq.push(arr{h[redge[i]] - h[i], i});
}
out = minima.size();
int d = 1;
arr a;
while (!pq.empty())
{
a = pq.top();
pq.pop();
if (removed[a[1]]) continue;
if (a[0] >= d) { // Might not get last
output.emplace_back(arr{d, out});
d = a[0] + 1;
}
// Left side
if (h[ledge[a[1]]] - h[a[1]] == a[0])
{
int i = ledge[a[1]] - 1;
while (i > 0 && owner[i] == 0 && h[i] <= h[ledge[a[1]]]) i--;
if (owner[i] != 0) {
uni(owner[i], a[1]);
if (ledge[owner[i]] != 0) pq.emplace(arr{h[ledge[owner[i]]] - h[owner[i]], owner[i]});
if (redge[owner[i]] != n + 1) pq.emplace(arr{h[redge[owner[i]]] - h[owner[i]], owner[i]});
} else if (h[i] > h[ledge[a[1]]])
{
ledge[a[1]] = i;
owner[i + 1] = a[1];
if (i != 0) pq.emplace(arr{h[i] - h[a[1]], a[1]});
}
}
// Right side
if (h[redge[a[1]]] - h[a[1]] == a[0])
{
int i = redge[a[1]] + 1;
while (i <= n && owner[i] == 0 && h[i] <= h[redge[a[1]]]) i++;
if (owner[i] != 0)
{
uni(a[1], owner[i]);
if (redge[a[1]] != n + 1) pq.emplace(arr{h[redge[a[1]]] - h[a[1]], a[1]});
if (ledge[a[1]] != 0) pq.emplace(arr{h[ledge[a[1]]] - h[a[1]], a[1]});
} else if (h[i] > h[redge[a[1]]])
{
redge[a[1]] = i;
owner[i - 1] = a[1];
if (i != n + 1) pq.emplace(arr{h[i] - h[a[1]], a[1]});
}
}
// cout << "a: " << a[0] << " " << a[1] << " " << ledge[a[1]] << " " << redge[a[1]] << "\n";
}
if (d != output.back()[0])
{
output.emplace_back(arr{d, out});
}
// for (auto &i : output) {
// cout << i[0] << " " << i[1] <<"\n";
// }
}
int max_towers(int l, int r, int d) {
if (r - l < 2) return 1;
auto it = lower_bound(output.begin(), output.end(), arr{d, (int)2e9}) - 1;
return (*it)[1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
357 ms |
1864 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
513 ms |
4912 KB |
1st lines differ - on the 1st token, expected: '11903', found: '33010' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
1408 KB |
Output is correct |
2 |
Correct |
733 ms |
4876 KB |
Output is correct |
3 |
Correct |
759 ms |
4888 KB |
Output is correct |
4 |
Correct |
705 ms |
5284 KB |
Output is correct |
5 |
Correct |
876 ms |
5184 KB |
Output is correct |
6 |
Correct |
651 ms |
5276 KB |
Output is correct |
7 |
Correct |
739 ms |
5256 KB |
Output is correct |
8 |
Correct |
801 ms |
2852 KB |
Output is correct |
9 |
Correct |
706 ms |
2880 KB |
Output is correct |
10 |
Correct |
661 ms |
2844 KB |
Output is correct |
11 |
Correct |
653 ms |
2852 KB |
Output is correct |
12 |
Correct |
35 ms |
4972 KB |
Output is correct |
13 |
Correct |
40 ms |
5216 KB |
Output is correct |
14 |
Correct |
45 ms |
5288 KB |
Output is correct |
15 |
Correct |
12 ms |
2852 KB |
Output is correct |
16 |
Correct |
12 ms |
2844 KB |
Output is correct |
17 |
Correct |
33 ms |
4756 KB |
Output is correct |
18 |
Correct |
39 ms |
4908 KB |
Output is correct |
19 |
Correct |
38 ms |
4992 KB |
Output is correct |
20 |
Correct |
48 ms |
5288 KB |
Output is correct |
21 |
Correct |
40 ms |
5272 KB |
Output is correct |
22 |
Correct |
40 ms |
5256 KB |
Output is correct |
23 |
Correct |
41 ms |
5288 KB |
Output is correct |
24 |
Correct |
12 ms |
2880 KB |
Output is correct |
25 |
Correct |
14 ms |
2872 KB |
Output is correct |
26 |
Correct |
17 ms |
2844 KB |
Output is correct |
27 |
Correct |
10 ms |
1712 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
296 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
288 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
432 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
357 ms |
1864 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |