#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long double ld;
const int maxn = 1e6 + 10;
const ld eps = 1e-9;
int n,h[maxn]; ld ans[maxn],ans2[maxn];
int p[maxn],q[maxn],k;
ld x[maxn];
ld f(int a, int b, int xx)
{
return 1.0*sqrt(xx-a) + b;
}
int bet(int id, int i, int j)
{
if (q[id]>=j) return n;
if (f(p[id],q[id],i)<j) return i-1;
int l=i; int r=n;
int ans=n;
while (l<=r)
{
int mid=(l+r)/2;
if (f(p[id],q[id],mid) - f(i,j,mid)>=eps)
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
return ans;
}
void add(int i, int j)
{
if (k==0)
{
k++;
p[k]=i; q[k]=j;
x[k]=n;
return;
}
x[k]=bet(k,i,j);
while (k>1&&x[k-1]>=x[k])
{
k--;
x[k]=bet(k,i,j);
}
k++;
p[k]=i; q[k]=j;
x[k]=n;
}
/**
f(x) = sqrt(x-a) + b
bet(
**/
ld qry(int idx)
{
int l=1; int r=k; int z=-1;
while (l<=r)
{
int mid=(l+r)/2;
if (x[mid]>=idx)
{
z=mid;
r=mid-1;
}
else
l=mid+1;
}
// cout<<idx<<" ::: "<<z<<'\n';
return f(p[z],q[z],idx);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin>>n;
for (int i=1; i<=n; i++) cin>>h[i];
k=0;
for (int i=1; i<=n; i++)
{
add(i-1,h[i]);
// for (int j=1; j<=k; j++) cout<<p[j]<<','<<q[j]<<". "<<x[j]<<" : "; cout<<'\n';
ans[i]=qry(i-1);
}
reverse(h+1,h+n+1); k=0;
for (int i=1; i<=n; i++)
{
add(i-1,h[i]);
// for (int j=1; j<=k; j++) cout<<p[j]<<','<<q[j]<<". "<<x[j]<<" : "; cout<<'\n';
ans2[n-i+1]=qry(i-1);
}
reverse(h+1,h+n+1);
for (int i=1; i<=n; i++)
{
int res = ceil(max(ans[i],ans2[i]))-h[i];
// cout<<ans[i]<<' '<<ans2[i]<<'\n';
cout<<res<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
3276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
3804 KB |
Output is correct |
2 |
Correct |
14 ms |
3284 KB |
Output is correct |
3 |
Correct |
17 ms |
3776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
5552 KB |
Output is correct |
2 |
Incorrect |
27 ms |
5552 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
12612 KB |
Output is correct |
2 |
Correct |
65 ms |
12160 KB |
Output is correct |
3 |
Correct |
52 ms |
12156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
20916 KB |
Output is correct |
2 |
Correct |
94 ms |
18936 KB |
Output is correct |
3 |
Correct |
86 ms |
19532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
29664 KB |
Output is correct |
2 |
Incorrect |
130 ms |
26852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
27316 KB |
Output is correct |
2 |
Incorrect |
132 ms |
26768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |