#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
using ii = pair <ll, ll>;
using vii = vector <ii>;
using vi = vector <int>;
ll n, k;
vll ve;
void init (int n, vi ve) {
::n = n;
k = 0;
::ve = vll(ve.begin(), ve.end());
while (ve[k] < ve[k+1]) k++;
}
const ll MAXN = 1E5+16;
ll dp[2][MAXN];
ll dpPs[2][MAXN];
// dp[0] low
// dp[1] up
int max_towers (int ql, int qr, int d) {
// fill(dp+ql, dp+qr+1, 0);
// cerr << d << '\n';
// for (ll i = ql; i <= qr; i++) {
// ii maxN = { 0, -16 };
// dp[i] = 1;
// for (ll j = i-1; j >= ql; j--) {
// if (ve[i] <= maxN.first-d)
// if (ve[j] <= maxN.first-d) {
// dp[i] = max(dp[i], dp[j]+1);
// }
// maxN = max(maxN, ii{ ve[j], j });
// }
// // cerr << ve[i] << ": " << dp[i] << '\n';
// }
// return *max_element(dp+ql, dp+qr+1);
// fill(dp+ql, dp+qr+1, 0);
deque <ii> dqUp, dqDw;
// dqUp sorted increasing
// dqDw sorted decreasing
for (ll i = ql; i <= qr; i++) {
dp[0][i] = 1;
dp[1][i] = 0;
while (dqUp.size() && dqUp.front().first < ve[i]) dqUp.pop_front();
while (dqDw.size() && dqDw.front().first > ve[i]) dqDw.pop_front();
// if (j < dq.size()) dp[i] = max(dp[i], dq[j].second+1);
// cerr << ve[i] << ": " << dp[i] << '\n';
// cerr << "j " << j << " ";
// for (auto [v1, v2] : dqUp) cerr << v1 << ' ' << v2 << " ";
// cerr << '\n';
// for (auto [v1, v2] : dqDw) cerr << v1 << ' ' << v2 << " ";
// cerr << '\n';
// cerr << '\n';
auto itUp = lower_bound(dqUp.begin(), dqUp.end(), ii{ ve[i]+d, 0 });
// for (auto [v, j] : dqUp) {
// if (ve[j] < ve[i]+d) continue;
// dp[0][i] = max(dp[0][i], dpPs[1][j]+1);
// assert(jUp == j);
// break;
// }
if (itUp != dqUp.end()) dp[0][i] = max(dp[0][i], dpPs[1][itUp->second]+1);
// cerr << jUp << ' ';
auto itDw = lower_bound(dqDw.begin(), dqDw.end(), ii{ ve[i]-d, 1E9 }, [&](ii a, ii b) { return a > b; });
// for (auto [v, j] : dqDw) {
// if (ve[j] > ve[i]-d) continue;
// dp[1][i] = max(dp[1][i], dpPs[0][j]+1);
// assert(jDw == j);
// break;
// }
if (itDw != dqDw.end()) dp[1][i] = max(dp[1][i], dpPs[0][itDw->second]+1);
// cerr << '\n';
dpPs[1][i] = max(dp[1][i], dqUp.size() ? dpPs[1][dqUp.front().second] : 0);
dqUp.push_front({ ve[i], i });
dpPs[0][i] = max(dp[0][i], dqDw.size() ? dpPs[0][dqDw.front().second] : 0);
dqDw.push_front({ ve[i], i });
// ll j = ll(lower_bound(dqUp.begin(), dqUp.end(), ii{ ve[i]+d, 0 }) - dq.begin())+1;
// for (ll j = i-1; j >= ql; j--) {
// if (ve[i] <= maxN.first-d)
// if (ve[j] <= maxN.first-d) dp[i] = max(dp[i], dp[j]+1);
// maxN = max(maxN, ii{ ve[j], j });
// }
}
return (*max_element(dp[0]+ql, dp[0]+qr+1)+1)/2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4062 ms |
3880 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
7 ms |
1624 KB |
Output is correct |
37 |
Correct |
11 ms |
2760 KB |
Output is correct |
38 |
Correct |
11 ms |
2000 KB |
Output is correct |
39 |
Correct |
10 ms |
2504 KB |
Output is correct |
40 |
Correct |
13 ms |
2000 KB |
Output is correct |
41 |
Correct |
15 ms |
4048 KB |
Output is correct |
42 |
Correct |
9 ms |
1976 KB |
Output is correct |
43 |
Correct |
20 ms |
4048 KB |
Output is correct |
44 |
Correct |
10 ms |
2244 KB |
Output is correct |
45 |
Correct |
9 ms |
1880 KB |
Output is correct |
46 |
Correct |
12 ms |
1872 KB |
Output is correct |
47 |
Correct |
12 ms |
2512 KB |
Output is correct |
48 |
Correct |
16 ms |
2256 KB |
Output is correct |
49 |
Correct |
10 ms |
1880 KB |
Output is correct |
50 |
Correct |
10 ms |
2692 KB |
Output is correct |
51 |
Correct |
18 ms |
3520 KB |
Output is correct |
52 |
Correct |
14 ms |
4560 KB |
Output is correct |
53 |
Correct |
13 ms |
4656 KB |
Output is correct |
54 |
Correct |
19 ms |
4560 KB |
Output is correct |
55 |
Correct |
20 ms |
6352 KB |
Output is correct |
56 |
Correct |
15 ms |
5352 KB |
Output is correct |
57 |
Correct |
14 ms |
4572 KB |
Output is correct |
58 |
Correct |
14 ms |
4544 KB |
Output is correct |
59 |
Correct |
14 ms |
4560 KB |
Output is correct |
60 |
Correct |
15 ms |
4560 KB |
Output is correct |
61 |
Correct |
14 ms |
4560 KB |
Output is correct |
62 |
Correct |
14 ms |
4644 KB |
Output is correct |
63 |
Correct |
14 ms |
4560 KB |
Output is correct |
64 |
Correct |
18 ms |
6188 KB |
Output is correct |
65 |
Correct |
18 ms |
6352 KB |
Output is correct |
66 |
Correct |
18 ms |
5584 KB |
Output is correct |
67 |
Correct |
21 ms |
6096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4094 ms |
4716 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4021 ms |
1368 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
7 ms |
1624 KB |
Output is correct |
37 |
Correct |
11 ms |
2760 KB |
Output is correct |
38 |
Correct |
11 ms |
2000 KB |
Output is correct |
39 |
Correct |
10 ms |
2504 KB |
Output is correct |
40 |
Correct |
13 ms |
2000 KB |
Output is correct |
41 |
Correct |
15 ms |
4048 KB |
Output is correct |
42 |
Correct |
9 ms |
1976 KB |
Output is correct |
43 |
Correct |
20 ms |
4048 KB |
Output is correct |
44 |
Correct |
10 ms |
2244 KB |
Output is correct |
45 |
Correct |
9 ms |
1880 KB |
Output is correct |
46 |
Correct |
12 ms |
1872 KB |
Output is correct |
47 |
Correct |
12 ms |
2512 KB |
Output is correct |
48 |
Correct |
16 ms |
2256 KB |
Output is correct |
49 |
Correct |
10 ms |
1880 KB |
Output is correct |
50 |
Correct |
10 ms |
2692 KB |
Output is correct |
51 |
Correct |
18 ms |
3520 KB |
Output is correct |
52 |
Correct |
14 ms |
4560 KB |
Output is correct |
53 |
Correct |
13 ms |
4656 KB |
Output is correct |
54 |
Correct |
19 ms |
4560 KB |
Output is correct |
55 |
Correct |
20 ms |
6352 KB |
Output is correct |
56 |
Correct |
15 ms |
5352 KB |
Output is correct |
57 |
Correct |
14 ms |
4572 KB |
Output is correct |
58 |
Correct |
14 ms |
4544 KB |
Output is correct |
59 |
Correct |
14 ms |
4560 KB |
Output is correct |
60 |
Correct |
15 ms |
4560 KB |
Output is correct |
61 |
Correct |
14 ms |
4560 KB |
Output is correct |
62 |
Correct |
14 ms |
4644 KB |
Output is correct |
63 |
Correct |
14 ms |
4560 KB |
Output is correct |
64 |
Correct |
18 ms |
6188 KB |
Output is correct |
65 |
Correct |
18 ms |
6352 KB |
Output is correct |
66 |
Correct |
18 ms |
5584 KB |
Output is correct |
67 |
Correct |
21 ms |
6096 KB |
Output is correct |
68 |
Execution timed out |
4094 ms |
4716 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4062 ms |
3880 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |