#include "towers.h"
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
namespace solve{
int N; vector<int> H;
map<int, int> ans;
void init(){
set<pair<int, int>> T;
set<pair<int, pair<int, int>>> S;
vector<bool> active(N, true);
for(int i = 0; i < N; i++){
if(i > 0 && i < N - 1 && ((H[i - 1] <= H[i] && H[i] <= H[i + 1]) || (H[i - 1] >= H[i] && H[i] >= H[i + 1]))) {
active[i] = false;
continue;
}
T.insert({i, H[i]});
}
if (T.size() >= 2 && (T.begin())->se > next(T.begin())->se) {
active[T.begin()->fi] = false;
T.erase(T.begin());
}
if (T.size() >= 2 && prev(T.end())->se > prev(prev(T.end()))->se) {
active[prev(T.end())->fi] = false;
T.erase(prev(T.end()));
}
/*
cout << "D = 0" << endl;
for(auto t : T){
cout << "[" << t.fi << "] => " << t.se << endl;
}
*/
ans[0] = (T.size() + 1) / 2;
for(auto t = T.begin(); t != T.end(); t++){
if(t != T.begin()) {
S.insert({abs(t->se - prev(t)->se), {t->fi, prev(t)->fi}});
}
}
vector<int> Ir;
while(S.size()){
auto [d, p] = *S.begin(); S.erase(S.begin());
Ir.push_back(d);
int x = p.fi, y = p.se;
if(!active[x] || !active[y])
continue;
active[x] = active[y] = false;
auto ptr = T.lower_bound({x, -1});
if(ptr != T.end() && ptr->fi == x)
T.erase(ptr);
ptr = T.lower_bound({y, -1});
if(ptr != T.end() && ptr->fi == y)
T.erase(ptr);
ptr = T.lower_bound({y, -1});
if (ptr != T.begin() && ptr != T.end()) {
S.insert({abs(ptr->se - prev(ptr)->se), {ptr->fi, prev(ptr)->fi}});
}
/*
cout << "D = " << d << endl;
for(auto t : T){
cout << "[" << t.fi << "] => " << t.se << endl;
}
*/
assert(T.size() % 2 == 1);
ans[d + 1] = (T.size() + 1) / 2;
}
}
int query(int L, int R, int D) {
return (--ans.upper_bound(D))->se;
}
};
void init(int N, vector<int> H) {
solve::N = N;
solve::H = H;
solve::init();
}
int max_towers(int L, int R, int D) {
return solve::query(L, R, D);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
408 ms |
976 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
715 ms |
9340 KB |
1st lines differ - on the 1st token, expected: '11903', found: '33010' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
307 ms |
2564 KB |
Output is correct |
2 |
Correct |
987 ms |
9352 KB |
Output is correct |
3 |
Correct |
998 ms |
9364 KB |
Output is correct |
4 |
Correct |
1209 ms |
13600 KB |
Output is correct |
5 |
Correct |
1146 ms |
13592 KB |
Output is correct |
6 |
Correct |
947 ms |
13484 KB |
Output is correct |
7 |
Correct |
906 ms |
13528 KB |
Output is correct |
8 |
Correct |
888 ms |
1352 KB |
Output is correct |
9 |
Correct |
747 ms |
1460 KB |
Output is correct |
10 |
Correct |
1020 ms |
1460 KB |
Output is correct |
11 |
Correct |
860 ms |
1488 KB |
Output is correct |
12 |
Correct |
90 ms |
9380 KB |
Output is correct |
13 |
Correct |
184 ms |
13580 KB |
Output is correct |
14 |
Correct |
158 ms |
13592 KB |
Output is correct |
15 |
Correct |
12 ms |
1440 KB |
Output is correct |
16 |
Correct |
13 ms |
1432 KB |
Output is correct |
17 |
Correct |
86 ms |
9136 KB |
Output is correct |
18 |
Correct |
102 ms |
9492 KB |
Output is correct |
19 |
Correct |
102 ms |
9492 KB |
Output is correct |
20 |
Correct |
160 ms |
13524 KB |
Output is correct |
21 |
Correct |
164 ms |
13592 KB |
Output is correct |
22 |
Correct |
164 ms |
13820 KB |
Output is correct |
23 |
Correct |
150 ms |
13592 KB |
Output is correct |
24 |
Correct |
16 ms |
1480 KB |
Output is correct |
25 |
Correct |
16 ms |
1456 KB |
Output is correct |
26 |
Correct |
11 ms |
1436 KB |
Output is correct |
27 |
Correct |
11 ms |
1444 KB |
Output is correct |
28 |
Correct |
2 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
464 KB |
Output is correct |
30 |
Correct |
2 ms |
464 KB |
Output is correct |
31 |
Correct |
1 ms |
208 KB |
Output is correct |
32 |
Correct |
1 ms |
208 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
2 ms |
464 KB |
Output is correct |
35 |
Correct |
2 ms |
464 KB |
Output is correct |
36 |
Correct |
2 ms |
464 KB |
Output is correct |
37 |
Correct |
2 ms |
464 KB |
Output is correct |
38 |
Correct |
2 ms |
464 KB |
Output is correct |
39 |
Correct |
2 ms |
464 KB |
Output is correct |
40 |
Correct |
1 ms |
208 KB |
Output is correct |
41 |
Correct |
1 ms |
208 KB |
Output is correct |
42 |
Correct |
1 ms |
208 KB |
Output is correct |
43 |
Correct |
1 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
408 ms |
976 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |