#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+7;
int n;
int h[N];
int L[N], R[N];
double sqr[N];
struct Sol {
int i, high;
Sol(int i, int high):i(i), high(high) {}
int value(int j) { return high + ceil(sqr[j-i]); }
double actual_value(int j) { return high + sqr[j-i]; }
int to(Sol &other) {
int lo = other.i, hi = n-1;
while (lo <= hi) {
int mid = lo+hi>>1;
if (actual_value(mid) > other.actual_value(mid)) lo = mid+1;
else hi = mid-1;
}
return lo;
}
};
void solve(int A[]) {
deque<Sol> dq;
int hi = -1;
for (int i = 0; i < n; i++) {
while (dq.size() >= 2 && dq[0].value(i) < dq[1].value(i))
dq.pop_front();
if (dq.size())
A[i] = dq.front().value(i);
if (hi < h[i]) {
Sol sol(i, h[i]);
while (dq.size() && dq.back().to(sol) <= i)
dq.pop_back();
while (dq.size() >= 2 && dq[dq.size()-2].to(dq.back()) >= dq.back().to(sol))
dq.pop_back();
dq.push_back(sol);
}
hi = max(hi, h[i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
memset(sqr,0,sizeof(sqr));
for (int i = 0; i < N; i++)
sqr[i] = sqrt(i);
for (int i = 0; i < n; i++)
cin>>h[i];
solve(L);
reverse(h, h+n);
solve(R);
reverse(h, h+n);
reverse(R, R+n);
stringstream ss;
for (int i = 0; i < n; i++)
ss << max(0, max(L[i],R[i])-h[i]) << endl;
cout << ss.str();
return 0;
}
Compilation message
pio.cpp: In member function 'int Sol::to(Sol&)':
pio.cpp:18:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = lo+hi>>1;
~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
5620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
6516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
6132 KB |
Output is correct |
2 |
Correct |
21 ms |
6008 KB |
Output is correct |
3 |
Correct |
26 ms |
6692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
6896 KB |
Output is correct |
2 |
Correct |
34 ms |
6772 KB |
Output is correct |
3 |
Incorrect |
36 ms |
8432 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
10372 KB |
Output is correct |
2 |
Correct |
61 ms |
9840 KB |
Output is correct |
3 |
Correct |
64 ms |
11880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
18784 KB |
Output is correct |
2 |
Correct |
94 ms |
12648 KB |
Output is correct |
3 |
Correct |
100 ms |
18960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
24916 KB |
Output is correct |
2 |
Correct |
128 ms |
16232 KB |
Output is correct |
3 |
Correct |
142 ms |
25180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
17760 KB |
Output is correct |
2 |
Correct |
132 ms |
16232 KB |
Output is correct |
3 |
Correct |
139 ms |
25044 KB |
Output is correct |