1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include <iostream> #include <deque> #include <cmath> #include <algorithm> #define maxn 500010 #define ll long long #define INF 100000000000000000 using namespace std;
int n, a[maxn];
double f[maxn]; inline double w(int x, int y) { return x >= y ? -1 : a[x] + sqrt(y - x); }
struct node { int l, r, k; };
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; deque<node> Q; Q.push_back({ 1, n, 0 }); for (int i = 1; i <= n; ++i) { while (!Q.empty() && Q.front().r < i) Q.pop_front(); f[i] = max(f[i], w(Q.front().k, i)); while (!Q.empty() && w(Q.back().k, Q.back().l) <= w(i, Q.back().l)) Q.pop_back(); int l = Q.back().l, r = Q.back().r, k = Q.back().k, mid, ans; while (l <= r) { mid = l + r >> 1; if (w(k, mid) > w(i, mid)) ans = mid, l = mid + 1; else r = mid - 1; } Q.back().r = ans; if (ans < n) Q.push_back({ ans + 1, n, i }); }
while (!Q.empty()) Q.pop_back(); Q.push_back({ 1, n, 0 }); reverse(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) { while (!Q.empty() && Q.front().r < i) Q.pop_front(); f[n - i + 1] = max(f[n - i + 1], w(Q.front().k, i)); while (!Q.empty() && w(Q.back().k, Q.back().l) <= w(i, Q.back().l)) Q.pop_back(); int l = Q.back().l, r = Q.back().r, k = Q.back().k, mid, ans; while (l <= r) { mid = l + r >> 1; if (w(k, mid) > w(i, mid)) ans = mid, l = mid + 1; else r = mid - 1; } Q.back().r = ans; if (ans < n) Q.push_back({ ans + 1, n, i }); } reverse(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) cout << max(0, (int) ceil(f[i]) - a[i]) << "\n"; return 0; }
|