Submission #1296449

#TimeUsernameProblemLanguageResultExecution timeMemory
1296449tranthienphuc2657Building Bridges (CEOI17_building)C++20
100 / 100
67 ms65400 KiB
// TranThienPhuc2657
// 27 ngay truoc khi toi ki thi Hoc sinh gioi Quoc gia 2025 - 2026, 25/12/2025.
#include <bits/stdc++.h>
using namespace std;
#define file "BUILD"
#define TIME 1.0 * clock() / CLOCKS_PER_SEC
#define ll long long
#define pb push_back
#define fi first
#define se second
#define pii pair <int, int>
#define pll pair <ll, ll>
#define Sz(x) ((int) (x).size())
#define getBit(mask, i) (((mask) >> (i)) & 1)
template <typename T1, typename T2> bool mini(T1 &A, T2 B) {if (A > B) A = B; else return 0; return 1;}
template <typename T1, typename T2> bool maxi(T1 &A, T2 B) {if (A < B) A = B; else return 0; return 1;}
const int inf = 2e9 + 7;
const ll linf = 1e18l + 7;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;

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

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

// init
void init() {
    for (int i = 1; i <= n; i++) sumW[i] = sumW[i - 1] + w[i];
}

// proc
namespace sub1 {
    // check
    bool check() {
        return n <= 1000;
    }

    ll dp[N];

    // init
    void init() {
        for (int i = 2; i <= n; i++) dp[i] = linf;
        dp[1] = 0;
    }

    // solve
    void solve() {
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j < i; j++) {
                mini(dp[i], - 2 * h[i] * h[j] + dp[j] + h[j] * h[j] - sumW[j] + h[i] * h[i] + sumW[i - 1]);
            }
        }
        cout << dp[n];
    }

    // proc
    void proc() {
        init();
        solve();
    }
}

namespace sub3 {
    const int MAX = 1e6;

    // check
    bool check() {
        return true;
    }

    struct Li_chao_tree {
        struct Line {
            ll a, b;

            Line() : a(0), b(linf) {}
            Line(ll a, ll b) : a(a), b(b) {}

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

        Line d[MAX * 4];

        void add(int id, int l, int r, Line cur) {
            int mid = (l + r) / 2;
            if (cur.eval(mid) < d[id].eval(mid)) swap(d[id], cur);
            if (l == r) return;

            if (cur.a == d[id].a) return;
            if (cur.a > d[id].a) add(id * 2, l, mid, cur);
            if (cur.a < d[id].a) add(id * 2 + 1, mid + 1, r, cur);
        }

        void add(ll a, ll b) {
            add(1, 0, MAX, Line(a, b));
        }

        ll get(int id, int l, int r, int pos) {
            ll cur = d[id].eval(pos);
            if (l == r) return cur;
            int mid = (l + r) / 2;
            if (pos <= mid) return min(cur, get(id * 2, l, mid, pos));
            return min(cur, get(id * 2 + 1, mid + 1, r, pos));
        }

        ll get(int pos) {
            return get(1, 0, MAX, pos);
        }
    } lichao;
    ll f = 0;

    // init
    void init() {
        lichao.add(-2 * h[1], h[1] * h[1] - sumW[1]);
    }

    // solve
    void solve() {
        for(int i = 2; i <= n; i++) {
            f = lichao.get(h[i]) + h[i] * h[i] + sumW[i - 1];
            lichao.add(-2 * h[i], f + h[i] * h[i] - sumW[i]);
        }
        cout << f;
    }

    // proc
    void proc() {
        init();
        solve();
    }
}

void proc() {
    if (sub1::check()) return sub1::proc();
    if (sub3::check()) return sub3::proc();
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(file".inp", "r")) {
        freopen(file".inp", "r", stdin);
        freopen(file".out", "w", stdout);
    }

    inp();
    init();
    proc();
    cerr << "Time elapsed: " << TIME << "s.\n";
    return 0;
}

Compilation message (stderr)

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