Submission #939394

# Submission time Handle Problem Language Result Execution time Memory
939394 2024-03-06T10:21:26 Z caterpillow Building Bridges (CEOI17_building) C++17
60 / 100
39 ms 9812 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pl = pair<ll, ll>;
#define vt vector
#define f first
#define s second
#define all(x) x.begin(), x.end() 
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1;i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
#define debug(x) do{auto _x = x; cerr << #x << " = " << _x << endl;} while(0)
const ll INF = 1e18;

struct Line {
    mutable ll a, b, p;
    bool operator<(const Line& o) const { return a < o.a; }
    bool operator<(ll x) const { return p < x; }
};

ll sign = -1; // -1 for min, 1 for max
struct LineContainer : multiset<Line, less<>> {
    // (for doubles, use inf = 1/.0, div(a,b) = a/b)
    ll div(ll a, ll b) { // floored division
        return a / b - ((a ^ b) < 0 && a % b); 
    }
    bool isect(auto x, auto y) {
        if (y == end()) { x->p = INF; return false; }
        if (x->a == y->a) x->p = x->b > y->b ? INF : -INF;
        else x->p = div(y->b - x->b, x->a - y->a);
        return x->p >= y->p;
    }
    void add(ll a, ll b) {
    a *= sign, b *= sign;
        auto z = insert({a, b, 0}), y = z++, x = y;
        while (isect(y, z)) z = erase(z);
        if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
        while ((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y));
    }
    ll query(ll x) {
        assert(size());
        auto l = *lower_bound(x);
        return -l.a * x - l.b;
    }
};

/*

assume all bridges are getting broken already and add back the ones that are not

dp[i] = min(h[i] - h[j]) ^ 2 + d[j]) - w[i]
dp[i] = h[i] ^ 2 - w[i] + min(-2 * h[i] * h[j] + h[j]^2 + dp[j])

*/

ll n;
vt<ll> h, w;

main() {    
    cin.tie(0)->sync_with_stdio(0);
    
    cin >> n;
    h = w = vt<ll>(n);
    F0R (i, n) cin >> h[i];
    F0R (i, n) cin >> w[i];

    vt<ll> dp(n);
    dp[0] = -w[0];

    LineContainer lc;
    lc.add(-2 * h[0], h[0] * h[0] + dp[0]);

    FOR (i, 1, n) {
        dp[i] = h[i] * h[i] - w[i] + lc.query(h[i]);
        lc.add(-2 * h[i], h[i] * h[i] + dp[i]);
    }

    cout << dp[n - 1] + accumulate(all(w), 0) << endl;
}

Compilation message

building.cpp:31:16: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   31 |     bool isect(auto x, auto y) {
      |                ^~~~
building.cpp:31:24: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   31 |     bool isect(auto x, auto y) {
      |                        ^~~~
building.cpp:63:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   63 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 3880 KB Output is correct
2 Correct 31 ms 3676 KB Output is correct
3 Correct 31 ms 3676 KB Output is correct
4 Correct 31 ms 3688 KB Output is correct
5 Correct 27 ms 4728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 31 ms 3880 KB Output is correct
7 Correct 31 ms 3676 KB Output is correct
8 Correct 31 ms 3676 KB Output is correct
9 Correct 31 ms 3688 KB Output is correct
10 Correct 27 ms 4728 KB Output is correct
11 Correct 30 ms 3928 KB Output is correct
12 Correct 31 ms 3676 KB Output is correct
13 Correct 24 ms 3684 KB Output is correct
14 Correct 31 ms 3920 KB Output is correct
15 Correct 39 ms 9812 KB Output is correct
16 Correct 27 ms 4696 KB Output is correct
17 Incorrect 16 ms 3688 KB Output isn't correct
18 Halted 0 ms 0 KB -