Submission #671805

# Submission time Handle Problem Language Result Execution time Memory
671805 2022-12-14T02:45:57 Z lam Lightning Conductor (POI11_pio) C++14
100 / 100
158 ms 35668 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define int long long
using namespace std;
typedef long double ld;
const int maxn = 1e6 + 10;
const ld eps = 1e-12;
int n,h[maxn]; ld ans[maxn],ans2[maxn],sq[maxn];
int p[maxn],q[maxn],k;
deque<pair<pair<int,int>,int>> D;
ld x[maxn];

ld f(int a, int b, int xx)
{
    return 1.0*sq[xx-a] + b;
}

int bet(pair<int,int> x, pair<int,int> y)
{
    if (x.ss>=y.ss) return n;
    if (f(x.ff,x.ss,y.ff)-y.ss<-eps) return y.ff-1;
    int l=y.ff; int r=n;
    int ans=-1;
    while (l<=r)
    {
        int mid=(l+r)/2;
        if (f(x.ff,x.ss,mid) - f(y.ff,y.ss,mid)>eps)
        {
            ans=mid;
            l=mid+1;
        }
        else
            r=mid-1;
    }
    return ans;
}

void add(int i, int j)
{
    while (1)
    {
        if (D.empty()) break;
        D.back().ss=bet(D.back().ff,{i,j});
        if (D.size()>1&&D[D.size()-2].ss>=D.back().ss) D.pop_back();
        else break;
    }
    D.push_back({{i,j},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';
        while (!D.empty()&&D[0].ss<i-1) D.pop_front();
        ans[i]=f(D[0].ff.ff,D[0].ff.ss,i-1);
    }
    reverse(h+1,h+n+1);
    while (!D.empty()) D.pop_back();
    for (int i=1; i<=n; i++)
    {
        add(i-1,h[i]);
        while (!D.empty()&&D[0].ss<i-1) D.pop_front();
        ans2[n-i+1]=f(D[0].ff.ff,D[0].ff.ss,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 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 10 ms 2260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 3540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 4120 KB Output is correct
2 Correct 17 ms 4180 KB Output is correct
3 Correct 17 ms 4436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 6136 KB Output is correct
2 Correct 30 ms 6012 KB Output is correct
3 Correct 22 ms 7244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 13876 KB Output is correct
2 Correct 69 ms 13672 KB Output is correct
3 Correct 53 ms 14408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 23024 KB Output is correct
2 Correct 89 ms 20940 KB Output is correct
3 Correct 78 ms 23028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 32676 KB Output is correct
2 Correct 154 ms 29644 KB Output is correct
3 Correct 137 ms 35668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 30188 KB Output is correct
2 Correct 158 ms 29736 KB Output is correct
3 Correct 136 ms 35632 KB Output is correct