// Author: Kajetan Ramsza
#include "bits/stdc++.h"
#include "towers.h"
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#ifdef DEBUG
auto &operator<<(auto &os, const pair<auto, auto> &p) {
return os<<"("<<p.first<<", "<<p.second<<")";
}
auto &operator<<(auto &os, const auto &v) {
os << "{";
for(auto it=begin(v);it!=end(v);it++) {
if(it != begin(v)) os<<", ";
os<<(*it);
}
return os<<"}";
}
void dbg_out(auto... x) {
((cerr<<' '<<x), ...) << endl;
}
#define dbg(x...) cerr<<"("<<#x<<"):", dbg_out(x)
#else
#define dbg(...)
#endif
int count_larger(const vi &vec, int x) {
dbg(vec);
int b = 0, e = sz(vec);
while(b < e) {
int mid = (b+e) / 2;
if(vec[mid] < x) b = mid + 1;
else e = mid;
}
return sz(vec) - b;
}
struct SegTree {
int n;
vector<vi> tree;
SegTree() {}
SegTree(vi vals) {
n = 1;
while(n < sz(vals))
n *= 2;
tree.assign(2*n, {});
rep(i,0,n)
if(i < sz(vals))
tree[n+i] = {vals[i]};
else
tree[n+i] = {-1};
dbg(tree);
for(int i=n-1;i>0;i--)
merge(all(tree[2*i]), all(tree[2*i+1]), back_inserter(tree[i]));
dbg(tree);
}
int query(int l, int r, int x) {
int res = 0;
for(l+=n, r+=n; l<r; l/=2, r/=2) {
if(l&1) res += count_larger(tree[l++], x);
if(r&1) res += count_larger(tree[--r], x);
}
return res;
}
};
const int inf = 2e9 + 7;
int n;
vi h;
vi sel;
vi tim;
vi lft, rht;
SegTree *tree;
void init(int N, vi H) {
n = N;
h = H;
sel.assign(n, 0);
int prev = -1;
bool small = false;
rep(i,0,n) {
if(small) {
if(prev != -1 && h[i] <= h[prev]) {
sel[prev] = false;
sel[i] = true;
prev = i;
}
else {
prev = i;
sel[i] = true;
small = false;
}
} else {
if(prev != -1 && h[i] >= h[prev]) {
sel[prev] = false;
sel[i] = true;
prev = i;
}
else {
prev = i;
sel[i] = true;
small = true;
}
}
}
dbg(sel);
set<pii> s;
tim.assign(n, -1);
lft.assign(n, -1);
rht.assign(n, -1);
prev = -1;
rep(i,0,n) {
if(!sel[i])
continue;
lft[i] = prev;
if(prev != -1) rht[prev] = i;
prev = i;
}
auto distance = [&](const int a, const int b) {
return (int)abs(h[a] - h[b]);
};
auto update = [&](const int i) {
s.erase({tim[i], i});
tim[i] = inf;
if(lft[i] != -1) tim[i] = min(tim[i], distance(i, lft[i]));
if(rht[i] != -1 && rht[rht[i]] != -1) tim[i] = min(tim[i], distance(i, rht[i]));
s.insert({tim[i], i});
};
int parity = 1;
rep(i,0,n) {
if(!sel[i])
continue;
if(parity) update(i);
parity ^= 1;
}
while(sz(s)) {
auto [t, i] = *s.begin();
s.erase(s.begin());
dbg(t, i);
if(lft[i] != -1 && t == distance(i, lft[i]) && lft[lft[i]] != -1) {
rht[lft[lft[i]]] = rht[i];
update(lft[lft[i]]);
}
if(rht[i] != -1 && t == distance(i, rht[i]) && rht[rht[i]] != -1) {
lft[rht[rht[i]]] = lft[i];
update(rht[rht[i]]);
}
dbg(tim);
}
tree = new SegTree(tim);
}
int max_towers(int L, int R, int D) {
int num = tree->query(0, n, D);
if(num == 0) return 1;
dbg(D, num);
return num;
// return (num + 1) / 2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
350 ms |
12888 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 |
344 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 |
344 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 |
549 ms |
25164 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 |
208 ms |
6232 KB |
Output is correct |
2 |
Correct |
737 ms |
25164 KB |
Output is correct |
3 |
Correct |
759 ms |
25164 KB |
Output is correct |
4 |
Correct |
765 ms |
25164 KB |
Output is correct |
5 |
Correct |
793 ms |
25228 KB |
Output is correct |
6 |
Correct |
809 ms |
25168 KB |
Output is correct |
7 |
Correct |
792 ms |
25128 KB |
Output is correct |
8 |
Correct |
708 ms |
25164 KB |
Output is correct |
9 |
Correct |
705 ms |
25076 KB |
Output is correct |
10 |
Correct |
713 ms |
25164 KB |
Output is correct |
11 |
Correct |
692 ms |
25164 KB |
Output is correct |
12 |
Correct |
55 ms |
25180 KB |
Output is correct |
13 |
Correct |
69 ms |
25264 KB |
Output is correct |
14 |
Correct |
69 ms |
25168 KB |
Output is correct |
15 |
Correct |
29 ms |
25164 KB |
Output is correct |
16 |
Correct |
28 ms |
25172 KB |
Output is correct |
17 |
Correct |
55 ms |
25164 KB |
Output is correct |
18 |
Correct |
66 ms |
25212 KB |
Output is correct |
19 |
Correct |
62 ms |
25192 KB |
Output is correct |
20 |
Correct |
70 ms |
25164 KB |
Output is correct |
21 |
Correct |
70 ms |
25104 KB |
Output is correct |
22 |
Correct |
72 ms |
25244 KB |
Output is correct |
23 |
Correct |
74 ms |
25172 KB |
Output is correct |
24 |
Correct |
35 ms |
25192 KB |
Output is correct |
25 |
Correct |
31 ms |
25112 KB |
Output is correct |
26 |
Correct |
36 ms |
25164 KB |
Output is correct |
27 |
Correct |
29 ms |
25164 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
2 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
2 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
1 ms |
600 KB |
Output is correct |
39 |
Correct |
2 ms |
600 KB |
Output is correct |
40 |
Correct |
1 ms |
600 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
600 KB |
Output is correct |
43 |
Correct |
1 ms |
684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 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 |
350 ms |
12888 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |