#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9+10;
struct func {
int h, i, l, r;
};
int n;
int mx[500000];
int mx2[500000];
int H[500000];
int eval(func f, int i) {
return f.h+(int)ceil(sqrt(i-f.i));
}
// returns when right part of func a intersects func b
int intersectRight(func a, func b) {
if (eval(a, n) >= eval(b, n)) return inf;
long long d = b.h-a.h;
if (b.i-a.i-d*d<0) return -1;
long double x = (double)(b.i-a.i-(long long)d*d)/2/d;
long long ans = ceil(x*x)+b.i;
assert(ans < n);
return ans;
}
void solveRight(int mx[]) {
stack<func> s;
for (int i = 0; i < n; i++) {
func f = {H[i], i, i, n-1};
while (!s.empty()) {
func f2 = s.top();
int intersect = intersectRight(f2, f);
//cout << f2.i << " intersects " << f.i << " at " << intersect << endl;
if (intersect <= f2.l) s.pop();
else {
if (intersect - 1 <= n-1) s.top().r = intersect - 1;
f.l = intersect;
break;
}
}
if (f.l <= n-1) s.push(f);
assert(f.l >= i);
}
while (!s.empty()) {
func f = s.top(); s.pop();
//cout << f.i << " is max from " << f.l << " to " << f.r << endl;
for (int i = f.l; i <= f.r; i++) mx[i] = max(mx[i], eval(f, i));
}
for (int i = 0; i < n; i++) {
//cout << "mx[" << i << "] = " << mx[i] << endl;
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> H[i]; mx[i] = H[i];
}
solveRight(mx);
for (int i = 0; i < n/2; i++) {
swap(H[i], H[n-i-1]);
}
solveRight(mx2);
for (int i = 0; i < n; i++) {
mx[i] = max(mx[i], mx2[n-1-i]);
}
for (int i = 0; i < n/2; i++) {
swap(H[i], H[n-i-1]);
}
for (int i = 0; i < n; i++) {
cout << mx[i]-H[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
1400 KB |
Output is correct |
2 |
Correct |
22 ms |
1408 KB |
Output is correct |
3 |
Incorrect |
36 ms |
1656 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
1912 KB |
Output is correct |
2 |
Correct |
76 ms |
1928 KB |
Output is correct |
3 |
Incorrect |
58 ms |
2424 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
4088 KB |
Output is correct |
2 |
Correct |
156 ms |
3960 KB |
Output is correct |
3 |
Incorrect |
123 ms |
4600 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
262 ms |
7948 KB |
Output is correct |
2 |
Correct |
253 ms |
5880 KB |
Output is correct |
3 |
Incorrect |
200 ms |
8056 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
376 ms |
11120 KB |
Output is correct |
2 |
Correct |
379 ms |
8128 KB |
Output is correct |
3 |
Incorrect |
282 ms |
11216 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
374 ms |
8696 KB |
Output is correct |
2 |
Correct |
369 ms |
8312 KB |
Output is correct |
3 |
Incorrect |
280 ms |
11128 KB |
Output isn't correct |