#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
int n;
int solve(int h1, int i1, int h2, int i2) {
int l = i2, r = n + 1, mid;
while (l < r - 1) {
mid = (l + r) / 2;
if (h1 + sqrt(mid - i1) < h2 + sqrt(mid - i2)) r = mid;
else l = mid;
}
return h1 + sqrt(l - i1) < h2 + sqrt(l - i2) ? l : r;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int i, cur = 0, tmp;
cin >> n;
vector<int> h(n), ans(n);
for (i = 0; i < n; i++) cin >> h[i];
vector<pair<int, int>> hull = {mp(0, 0)};
for (i = 1; i < n; i++) {
while (h[i] + sqrt(hull.back().second - i) >
h[hull.back().first] + sqrt(hull.back().second - hull.back().first)) hull.pop_back();
tmp = solve(h[hull.back().first], hull.back().first, h[i], i);
if (tmp == n + 1) continue;
hull.push_back(mp(i, tmp));
}
for (i = 1; i < n; i++) {
while (cur < hull.size() - 1 and hull[cur + 1].second < i) cur++;
ans[i] = max(ans[i], (int) ceil(h[hull[cur].first] + sqrt(i - hull[cur].first)));
}
reverse(h.begin(), h.end());
reverse(ans.begin(), ans.end());
cur = 0;
hull = {mp(0, 0)};
for (i = 1; i < n; i++) {
while (h[i] + sqrt(hull.back().second - i) >
h[hull.back().first] + sqrt(hull.back().second - hull.back().first)) hull.pop_back();
tmp = solve(h[hull.back().first], hull.back().first, h[i], i);
if (tmp == n + 1) continue;
hull.push_back(mp(i, tmp));
}
for (i = 1; i < n; i++) {
while (cur < hull.size() - 1 and hull[cur + 1].second < i) cur++;
ans[i] = max(ans[i], (int) ceil(h[hull[cur].first] + sqrt(i - hull[cur].first)));
}
reverse(ans.begin(), ans.end());
reverse(h.begin(), h.end());
for (i = 0; i < n; i++) cout << max(0, ans[i] - h[i]) << endl;
return 0;
}
Compilation message
pio.cpp: In function 'int main()':
pio.cpp:35:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | while (cur < hull.size() - 1 and hull[cur + 1].second < i) cur++;
| ~~~~^~~~~~~~~~~~~~~~~
pio.cpp:52:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | while (cur < hull.size() - 1 and hull[cur + 1].second < i) cur++;
| ~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
1196 KB |
Output is correct |
2 |
Correct |
102 ms |
1020 KB |
Output is correct |
3 |
Correct |
102 ms |
1352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
1624 KB |
Output is correct |
2 |
Correct |
157 ms |
1488 KB |
Output is correct |
3 |
Correct |
166 ms |
1992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
390 ms |
3300 KB |
Output is correct |
2 |
Correct |
384 ms |
3104 KB |
Output is correct |
3 |
Correct |
387 ms |
3852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
598 ms |
6528 KB |
Output is correct |
2 |
Correct |
576 ms |
4692 KB |
Output is correct |
3 |
Correct |
584 ms |
6608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
850 ms |
9484 KB |
Output is correct |
2 |
Correct |
836 ms |
6416 KB |
Output is correct |
3 |
Correct |
843 ms |
9520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
828 ms |
6668 KB |
Output is correct |
2 |
Correct |
841 ms |
6392 KB |
Output is correct |
3 |
Correct |
837 ms |
9240 KB |
Output is correct |