Submission #892738

# Submission time Handle Problem Language Result Execution time Memory
892738 2023-12-25T19:39:49 Z tvladm2009 Building Bridges (CEOI17_building) C++17
100 / 100
51 ms 8920 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

const ll INF = 4e18;

struct Line {
    ll a;
    ll b;
    
    ll eval(int x) {
        return a * x + b;
    }
};

namespace LiChao {
    struct Node {
        Line val;
        Node* left;
        Node* right;
        Node(Line val_ = {0, 0}) {
            val = val_;
            left = right = NULL;
        }
    };
    void update(Node* &root, int from, int to, Line val) {
        if (root == NULL) {
            root = new Node(val);
        } else {
            int mid = (from + to) / 2;
            if (val.eval(mid) < root->val.eval(mid)) {
                swap(val, root->val);
            } 
            if (val.eval(from) < root->val.eval(from)) {
                update(root->left, from, mid, val);
            }
            if (val.eval(to) < root->val.eval(to)) {
                update(root->right, mid + 1, to, val);
            }
        }
    }
    ll query(Node* root, int from, int to, int x) {
        if (root == NULL) {
            return INF;
        } else {
            int mid = (from + to) / 2;
            if (x <= mid) {
                return min(query(root->left, from, mid, x), root->val.eval(x));
            } else {
                return min(query(root->right, mid + 1, to, x), root->val.eval(x));
            }
        }
    }
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<ll> h(n + 1), w(n + 1);
    for (int i = 1; i <= n; i += 1) {
        cin >> h[i];
    }
    vector<ll> pref(n + 1, 0);
    for (int i = 1; i <= n; i += 1) {
        cin >> w[i];
        pref[i] = pref[i - 1] + w[i];
    }
    vector<ll> dp(n + 1, INF);
    LiChao::Node* root = NULL;
    dp[1] = 0;
    for (int i = 1; i <= n; i += 1) {
        if (i != 1) {
            dp[i] = LiChao::query(root, 1, 1e6, h[i]);
            dp[i] += pref[i - 1] + h[i] * h[i];
        }
        int a = -2 * h[i];
        ll b = dp[i] + h[i] * h[i] - pref[i];
        LiChao::update(root, 1, 1e6, {a, b});
    }
    cout << dp[n] << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 3832 KB Output is correct
2 Correct 34 ms 4700 KB Output is correct
3 Correct 34 ms 4700 KB Output is correct
4 Correct 33 ms 4600 KB Output is correct
5 Correct 25 ms 5968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 51 ms 3832 KB Output is correct
7 Correct 34 ms 4700 KB Output is correct
8 Correct 34 ms 4700 KB Output is correct
9 Correct 33 ms 4600 KB Output is correct
10 Correct 25 ms 5968 KB Output is correct
11 Correct 34 ms 5080 KB Output is correct
12 Correct 34 ms 4700 KB Output is correct
13 Correct 26 ms 4712 KB Output is correct
14 Correct 32 ms 4944 KB Output is correct
15 Correct 29 ms 8920 KB Output is correct
16 Correct 25 ms 6052 KB Output is correct
17 Correct 14 ms 4440 KB Output is correct
18 Correct 13 ms 4444 KB Output is correct