Submission #671809

# Submission time Handle Problem Language Result Execution time Memory
671809 2022-12-14T02:49:58 Z lam Lightning Conductor (POI11_pio) C++14
100 / 100
157 ms 32716 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 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 10 ms 2300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 3540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 4224 KB Output is correct
2 Correct 16 ms 4180 KB Output is correct
3 Correct 19 ms 4436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 6092 KB Output is correct
2 Correct 36 ms 6092 KB Output is correct
3 Correct 29 ms 6644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 13932 KB Output is correct
2 Correct 73 ms 13768 KB Output is correct
3 Correct 68 ms 14436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 23072 KB Output is correct
2 Correct 109 ms 20952 KB Output is correct
3 Correct 107 ms 23052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 32612 KB Output is correct
2 Correct 157 ms 29704 KB Output is correct
3 Correct 115 ms 32716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 30284 KB Output is correct
2 Correct 153 ms 29692 KB Output is correct
3 Correct 120 ms 32700 KB Output is correct