Submission #798706

# Submission time Handle Problem Language Result Execution time Memory
798706 2023-07-31T01:16:50 Z 12345678 Building Bridges (CEOI17_building) C++17
0 / 100
32 ms 2056 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const ll nx=1e5+5, inf=1e18;
ll n, h[nx], w[nx], sm;
bool querymode;

struct line
{
    ll m, c;
    mutable ll p;
    line(ll m, ll c, ll p): m(m), c(c), p(p){}
    bool operator<(const line &o) const {return querymode?p<o.p:m>=o.m;}
};

struct convexhull:multiset<line>
{
    ll div(ll a, ll b)
    {
        if (a%b==0||(a^b)>=0) return a/b;
        return a/b-1;
    }
    bool isect(iterator x, iterator y)
    {
        if (y==end()) return x->p=inf, false;
        if (x->m==y->m) x->p=x->c>=y->c?inf:-inf;
        else x->p=div(x->c-y->c, y->m-x->m);
        return x->p>=y->p;
    }
    void add(ll m, ll c)
    {
        auto x=insert(line(m, c, 0)), y=next(x);
        while (isect(x, y)) y=erase(next(x));
        if ((y=x)!=begin()&&isect(--x, y)) isect(x, erase(y));
        while ((y=x)!=begin()&&(--x)->p>=y->p) isect(x, erase(y));
    }
    ll query(ll x)
    {
        if (empty()) return -inf;
        querymode=1;
        auto l=lower_bound(line(0, 0, x));
        querymode=0;
        return l->m*x+l->c;
    }
} c;

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (int i=1; i<=n; i++) cin>>h[i];
    for (int i=1; i<=n; i++) cin>>w[i], sm+=w[i];
    c.add(-2*h[1], h[1]*h[1]-w[1]);
    for (int i=2; i<=n-1; i++)
    {
        ll tmp=c.query(h[i])+h[i]*h[i]-w[i];
        c.add(-2*h[i], tmp+h[i]*h[i]);
    }
    cout<<c.query(h[n])+h[n]*h[n]-w[n]+sm;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 2056 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -