Submission #1245267

#TimeUsernameProblemLanguageResultExecution timeMemory
1245267enxiayyBuilding Bridges (CEOI17_building)C++20
0 / 100
59 ms73032 KiB
#include <bits/stdc++.h>
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define compact(v) v.erase(unique(all(v)), v.end())
#define dbg(v) "[" #v " = " << (v) << "]"
#define el "\n"

using namespace std;
typedef long long ll;
typedef long double ld;

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
long long randRange(long long l, long long r){ return uniform_int_distribution<long long>(l,r)(rng); }

const int N = 1e6 + 5;

int n;
ll h[N], w[N], pref[N];

struct Line {
    ll a, b;
    Line (ll a = 0, ll b = 0) : a(a), b(b) {}

    ll f(ll x) {
        return a * x + b;
    }

    ld intersect(Line Other) {
        return 1.0 * (Other.b - b) / (a - Other.a);
    }

    ll slope() {
        return a;
    }
};

struct Lichao {
    vector <Line> st;
    int n;

    Lichao(int n) : n(n), st(4 * n + 5, LLONG_MAX) {}

    void update(int id, int l, int r, Line val) {
        if (l == r) {
            st[id] = (val.f(l) < st[id].f(l) ? val : st[id]);
            return;
        }

        int mid = (l + r) >> 1;
        if (val.f(mid) < st[id].f(mid))
            swap(st[id], val);

        if (val.slope() > st[id].slope()) {
            update(id << 1, l, mid, val);
        }
        else if (val.slope() < st[id].slope()) {
            update(id << 1 | 1, mid  + 1, r, val);
        }
    }

    ll get(int id, int l, int r, ll x) {
        if (l == r) {
            return st[id].f(x);
        }

        int mid = (l + r) >> 1;

        ll cur = st[id].f(x);
        if (x <= mid) {
            return min(cur, get(id << 1, l, mid, x));
        }
        else {
            return min(cur, get(id << 1 | 1, mid + 1, r, x));
        }
    }

    void upd(Line val) {
        update(1, 1, n, val);
    }

    ll query(ll x) {
        return get(1, 1, n, x);
    }
};

ll dp[N];

void solve() {
    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> h[i];
    for(int i = 1; i <= n; ++i) {
        cin >> w[i];
        pref[i] = pref[i - 1] + w[i];
    }

    memset(dp, 0x3f, sizeof(dp));
    dp[1] = 0;

    // for(int i = 1; i <= n; ++i) {
    //     for(int j = 1; j < i; ++j) {
    //         dp[i] = min(dp[i], -2 * h[j] * h[i] + dp[j] + h[j] * h[j] - pref[j] + h[i] * h[i] + pref[i - 1]); 
    //     }
    // }

    // cout << dp[n] << el;

    Lichao lct(N);
    lct.upd(Line(-2 * h[1], dp[1] + h[1] * h[1] - pref[1]));
    for(int i = 1; i <= n; ++i) {
        dp[i] = lct.query(h[i]) + h[i] * h[i] + pref[i - 1];
        lct.upd(Line(-2 * h[i], dp[i] + h[i] * h[i] - pref[i]));
    }

    cout << dp[n] << el;

    
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    #define task "task" 
    if(fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    solve();

    return 0;
}

/*

*/

Compilation message (stderr)

building.cpp: In function 'int main()':
building.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...