제출 #720429

#제출 시각아이디문제언어결과실행 시간메모리
720429CookieBuilding Bridges (CEOI17_building)C++14
100 / 100
85 ms18096 KiB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("boards.in");
ofstream fout("boards.out");
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const ll mod = 1e9 + 7;
const int mxn = 2e5, mxm = 1e5;
vt<ll>comp;
struct Line{
    ll m, c;
    Line(){
        m = 0; c = 1e17;
    }
    Line(ll _m, ll _c){
        m = _m; c = _c;
    }
    ll val(ll x){return(x * m + c);}
};
int n;
Line st[4 * mxn + 1];

    void upd(int nd, int l, int r, Line curr){
        if(l == r){
            if(st[nd].val(comp[l]) > curr.val(comp[l])){
                swap(st[nd], curr);
            }
            return;
        }
        int mid = (l + r) >> 1;
        if(st[nd].m > curr.m)swap(st[nd], curr);
        if(st[nd].val(comp[mid]) > curr.val(comp[mid])){
            swap(st[nd], curr);
            upd(nd << 1 | 1, mid + 1, r, curr);
        }else{
            upd(nd << 1, l, mid, curr);
        }
    }
    ll get(int nd, int l, int r, ll x){
        if(l == r){
            return(st[nd].val(x));
        }
        int mid = (l + r) >> 1;
        if(x <= comp[mid]){
            return(min(st[nd].val(x), get(nd << 1, l, mid, x)));
        }else{
            return(min(st[nd].val(x), get(nd << 1 | 1, mid + 1, r, x)));
        }
    }
int find(int x){
    return(lower_bound(comp.begin(), comp.end(), x) - comp.begin());
}
ll dp[mxn + 1], w[mxn + 1], h[mxn + 1], p[mxn + 1];
signed main(){
    
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    forr(i, 1, n + 1){
        cin >> h[i]; comp.pb(h[i]);
    }
    sort(comp.begin(), comp.end());
    forr(i, 1, n + 1){
        cin >> w[i]; p[i] = p[i - 1] + w[i];
    }
    int sz = comp.size();
    dp[1] = 0;
    Line curr = Line(-2 * h[1], dp[1] + h[1] * h[1] - p[1]);
    upd(1, 0, sz - 1, curr);
    for(int i = 2; i <= n; i++){
        dp[i] = get(1, 0, sz - 1, h[i]) + h[i] * h[i] + p[i - 1];
        Line curr = Line(-2 * h[i], dp[i] + h[i] * h[i] - p[i]);
        upd(1, 0, sz - 1, curr);
        
    }
    cout << dp[n];
    return(0);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...