This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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++;
| ~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |