Submission #897385

# Submission time Handle Problem Language Result Execution time Memory
897385 2024-01-03T03:07:57 Z Wobert Palembang Bridges (APIO15_bridge) C++17
100 / 100
50 ms 11272 KB
#include "bits/stdc++.h"
#ifdef LOCAL
#include "dbg.h"
#else
#define dbg(...) 0
#endif

#define fox(i, n) for (int i = 0; i < (n); ++i)
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define rin(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define fore(x, v) for (auto &&x : v)
#define forp(a, b, v) for (auto &&[a, b] : v)
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define EL "\n"

using namespace std;

using vi = vector<int>;
using ll = long long;
using vll = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<>>;

template<class T>
int sz(T& container) {
    return (int) container.size();
}
template<class T> bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0; }

ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); }
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); }

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//ll urand(int a, int b) { return rng() % (b-a+1) + a; }

//const vector<pii> deltas = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const int INF = 0x3f3f3f3f;

template<int MOD, int RT> struct mint {
    static const int mod = MOD;
    static constexpr mint rt() { return RT; } // primitive root for FFT
    int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int
    mint():v(0) {}
    mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);
        if (v < 0) v += MOD; }
    bool operator==(const mint& o) const {
        return v == o.v; }
    friend bool operator!=(const mint& a, const mint& b) {
        return !(a == b); }
    friend bool operator<(const mint& a, const mint& b) {
        return a.v < b.v; }

    mint& operator+=(const mint& o) {
        if ((v += o.v) >= MOD) v -= MOD;
        return *this; }
    mint& operator-=(const mint& o) {
        if ((v -= o.v) < 0) v += MOD;
        return *this; }
    mint& operator*=(const mint& o) {
        v = int((ll)v*o.v%MOD); return *this; }
    mint& operator/=(const mint& o) { return (*this) *= inv(o); }
    friend mint pow(mint a, ll p) {
        mint ans = 1; assert(p >= 0);
        for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans; }
    friend mint inv(const mint& a) { assert(a.v != 0);
        return pow(a,MOD-2); }

    mint operator-() const { return mint(-v); }
    mint& operator++() { return *this += 1; }
    mint& operator--() { return *this -= 1; }
    friend mint operator+(mint a, const mint& b) { return a += b; }
    friend mint operator-(mint a, const mint& b) { return a -= b; }
    friend mint operator*(mint a, const mint& b) { return a *= b; }
    friend mint operator/(mint a, const mint& b) { return a /= b; }

    friend istream& operator>> (istream& is, mint& a) {
        is >> a.v;
        return is;
    }
    friend ostream& operator<< (ostream& os, const mint& a) {
        os << a.v;
        return os;
    }
};

using mi = mint<int(1e9+7), 5>;

struct bridge {
    pq<ll> left; // sz(left) >= sz(right)
    pqg<ll> right;
    ll cost{};
    // median = left.top()

    void adjust(bool l) {
        ll old, dif;
        if (l) {
            if (sz(left) - sz(right) <= 1) return;
            old = left.top(); left.pop();
            dif = left.top() - old;
        } else {
            if (sz(left) >= sz(right)) return;
            old = right.top(); right.pop();
            dif = old - left.top();
        }
        cost -= dif;
        cost += dif * sz(left);
        cost -= dif * sz(right);
        if (l) right.push(old);
        else left.push(old);

        assert(0 <= sz(left) - sz(right) && sz(left) - sz(right) <= 1);
    }

    void insert(ll p) {
        if (left.empty()) {
            left.push(p); return;
        }
        cost += abs(p - left.top());
        if (p > left.top()) {
            right.push(p);
            adjust(false);
        } else {
            left.push(p);
            adjust(true);
        }
    }

    void insert(pll p) {
        insert(p.F); insert(p.S);
    }
};

void test() {
    bridge br;
    while (true) {
        ll x; cin >> x;
        br.insert(x);
        dbg(br.cost);
    }
}

void solve() { // test();
    int k, n; cin >> k >> n;
    ll ans = 0;
    vpll v;
    fox(i, n) {
        char a, b; ll x, y; cin >> a >> x >> b >> y;
        if (a == b) ans += abs(x-y);
        else {
            v.eb(minmax(x, y)); ++ans;
        }
    }
    sort(all(v), [](pll a, pll b) {return a.F+a.S < b.F+b.S;});

    n = sz(v);
    vll lcost(n+1), rcost(n+1);
    bridge l, r;
    rin(i, 1, n) {
        l.insert(v[i-1]);
        r.insert(v[n-i]);
        lcost[i] = l.cost;
        rcost[i] = r.cost;
    }
    ll best;
    assert(lcost[n] == rcost[n]);
    if (k == 1) {
        best = lcost[n];
    } else {
        best = 1e18;
        rin(i, 0, n)
            ckmin(best, lcost[i] + rcost[n-i]);
    }
    cout << best + ans << EL;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
//    int t;
//    cin >> t;
//    while (t--)
        solve();
    return 0;
}

Compilation message

bridge.cpp: In function 'void test()':
bridge.cpp:5:18: warning: statement has no effect [-Wunused-value]
    5 | #define dbg(...) 0
      |                  ^
bridge.cpp:152:9: note: in expansion of macro 'dbg'
  152 |         dbg(br.cost);
      |         ^~~
# 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 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 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 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 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 24 ms 8404 KB Output is correct
13 Correct 50 ms 8344 KB Output is correct
14 Correct 39 ms 8136 KB Output is correct
15 Correct 33 ms 4656 KB Output is correct
16 Correct 24 ms 8916 KB Output is correct
17 Correct 41 ms 8816 KB Output is correct
18 Correct 25 ms 7908 KB Output is correct
19 Correct 46 ms 8812 KB Output is correct
20 Correct 32 ms 8144 KB Output is correct
21 Correct 45 ms 8908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 456 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 25 ms 9056 KB Output is correct
26 Correct 37 ms 8648 KB Output is correct
27 Correct 47 ms 10332 KB Output is correct
28 Correct 50 ms 10184 KB Output is correct
29 Correct 49 ms 10268 KB Output is correct
30 Correct 26 ms 5388 KB Output is correct
31 Correct 22 ms 10708 KB Output is correct
32 Correct 42 ms 11272 KB Output is correct
33 Correct 26 ms 10072 KB Output is correct
34 Correct 47 ms 11204 KB Output is correct
35 Correct 32 ms 10060 KB Output is correct
36 Correct 45 ms 10932 KB Output is correct
37 Correct 22 ms 9684 KB Output is correct