#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],sq[maxn];
int p[maxn],q[maxn],k;
ld x[maxn];
ld f(int a, int b, int xx)
{
return 1.0*sq[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=0; i<=n; i++) sq[i]= 1.0*sqrt(i);
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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
4252 KB |
Output is correct |
2 |
Correct |
13 ms |
4156 KB |
Output is correct |
3 |
Correct |
19 ms |
4428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
6108 KB |
Output is correct |
2 |
Incorrect |
24 ms |
6032 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
13888 KB |
Output is correct |
2 |
Correct |
57 ms |
13692 KB |
Output is correct |
3 |
Correct |
50 ms |
14376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
22988 KB |
Output is correct |
2 |
Correct |
90 ms |
20940 KB |
Output is correct |
3 |
Correct |
79 ms |
23180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
32684 KB |
Output is correct |
2 |
Incorrect |
123 ms |
29764 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
120 ms |
30220 KB |
Output is correct |
2 |
Incorrect |
122 ms |
29760 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |