Submission #53788285


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t) - 1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T>
bool chmax(T& a, const T& b) {
    return a < b ? (a = b, 1) : 0;
}
template <typename T>
bool chmin(T& a, const T& b) {
    return a > b ? (a = b, 1) : 0;
}

template <typename F>
long long golden_section_search(F f, long long lb, long long ub) {
    // t-s, s, t are consecutive fibonacci numbers
    long long s = 1, t = 2;
    while (t < ub - lb + 2) std::swap(s += t, t);
    long long l = lb - 1, m1 = l + t - s, m2 = l + s;
    auto v1 = f(m1), v2 = f(m2);
    while (m1 != m2) {
        std::swap(s, t -= s);
        if (ub < m2 || v1 <= v2) {
            m2 = m1;
            v2 = v1;
            m1 = l + t - s;
            v1 = f(m1);
        } else {
            l = m1;
            m1 = m2;
            v1 = v2;
            m2 = l + s;
            v2 = m2 <= ub ? f(m2) : v1;
        }
    }
    return m1;
}


template <typename T, typename F>
std::vector<T> monge_shortest_path(int N, const F& cost) {
    const T INF = std::numeric_limits<T>::max() / 2;
    std::vector<T> dist(N, INF);

    auto f = [&](int i, int j) { return dist[i] + cost(i, j); };

    // (f(i, *), intersection with the previous function)
    std::deque<std::pair<int, int>> dq;
    auto intersection = [&](int i1, int i2) {
        if (f(i1, i2) >= f(i2, i2)) return -1;
        int lb = i2, ub = N;
        while (ub - lb > 1) {
            int m = (lb + ub) / 2;
            if (f(i1, m) <= f(i2, m)) {
                lb = m;
            } else {
                ub = m;
            }
        }
        return lb;
    };
    auto add = [&](int i) {
        int x = -1;
        while (dq.size() >= 2 &&
               dq.back().second >= (x = intersection(dq.back().first, i))) {
            dq.pop_back();
        }
        if (dq.size() == 1) x = intersection(dq.back().first, i);
        dq.push_back({i, x});
    };
    auto get = [&](int i) {
        while (dq.size() >= 2 && f(dq.front().first, i) > f(dq[1].first, i)) {
            dq.pop_front();
        }
        dq.front().second = -1;
        return f(dq.front().first, i);
    };

    dist[0] = 0;
    add(0);
    for (int i = 1; i < N; ++i) {
        dist[i] = get(i);
        add(i);
    }
    return dist;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, K;
    cin >> N >> K;
    vector<ll> P(N + 1);
    rep(i, 1, N + 1) cin >> P[i];

    vector<ll> P0(N + 2), P1(N + 2);
    rep(i, 0, N + 1) {
        P0[i + 1] = P0[i] + P[i];
        P1[i + 1] = P1[i] + P[i] * i;
    }

    auto cost = [&](int i, int j) {
        if (i == 0 && j == N + 1) return (ll)1e18;
        if (i == 0) {
            return j * P0[j] - P1[j];
        }
        if (j == N + 1) {
            return (P1[N + 1] - P1[i]) - i * (P0[N + 1] - P0[i]);
        }
        int m = (i + j) / 2 + 1;
        return (P1[m] - P1[i]) - i * (P0[m] - P0[i]) + j * (P0[j] - P0[m]) -
               (P1[j] - P1[m]);
    };

    auto calc = [&](ll lambda) {
        auto dist = monge_shortest_path<ll>(
            N + 2, [&](int i, int j) { return cost(i, j) + lambda; });
        return dist[N + 1] - lambda * (K + 1);
    };

    ll lambda = golden_section_search([&](ll lambda) { return -calc(lambda); },
                                      0, 3e10);
    cout << calc(lambda) << endl;
}

Submission Info

Submission Time
Task G - Baseball
User sotanishy
Language C++ 20 (gcc 12.2)
Score 650
Code Size 3671 Byte
Status AC
Exec Time 900 ms
Memory 4736 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 650 / 650
Status
AC × 4
AC × 52
Set Name Test Cases
Sample 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 00_sample_04.txt
All 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 00_sample_04.txt, 01_random-small_01.txt, 01_random-small_02.txt, 01_random-small_03.txt, 01_random-small_04.txt, 01_random-small_05.txt, 01_random-small_06.txt, 01_random-small_07.txt, 01_random-small_08.txt, 01_random-small_09.txt, 01_random-small_10.txt, 01_random-small_11.txt, 01_random-small_12.txt, 01_random-small_13.txt, 01_random-small_14.txt, 01_random-small_15.txt, 01_random-small_16.txt, 01_random-small_17.txt, 01_random-small_18.txt, 01_random-small_19.txt, 01_random-small_20.txt, 02_handmade-small_01.txt, 02_handmade-small_02.txt, 02_handmade-small_03.txt, 02_handmade-small_04.txt, 03_random-large_01.txt, 03_random-large_02.txt, 03_random-large_03.txt, 03_random-large_04.txt, 03_random-large_05.txt, 03_random-large_06.txt, 03_random-large_07.txt, 03_random-large_08.txt, 03_random-large_09.txt, 03_random-large_10.txt, 03_random-large_11.txt, 03_random-large_12.txt, 03_random-large_13.txt, 03_random-large_14.txt, 03_random-large_15.txt, 03_random-large_16.txt, 03_random-large_17.txt, 03_random-large_18.txt, 03_random-large_19.txt, 03_random-large_20.txt, 04_handmade-large_01.txt, 04_handmade-large_02.txt, 04_handmade-large_03.txt, 04_handmade-large_04.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 1 ms 3624 KiB
00_sample_02.txt AC 1 ms 3620 KiB
00_sample_03.txt AC 1 ms 3476 KiB
00_sample_04.txt AC 1 ms 3428 KiB
01_random-small_01.txt AC 12 ms 3456 KiB
01_random-small_02.txt AC 7 ms 3392 KiB
01_random-small_03.txt AC 13 ms 3464 KiB
01_random-small_04.txt AC 3 ms 3372 KiB
01_random-small_05.txt AC 13 ms 3520 KiB
01_random-small_06.txt AC 9 ms 3520 KiB
01_random-small_07.txt AC 12 ms 3376 KiB
01_random-small_08.txt AC 11 ms 3372 KiB
01_random-small_09.txt AC 12 ms 3524 KiB
01_random-small_10.txt AC 2 ms 3436 KiB
01_random-small_11.txt AC 13 ms 3432 KiB
01_random-small_12.txt AC 6 ms 3360 KiB
01_random-small_13.txt AC 12 ms 3520 KiB
01_random-small_14.txt AC 4 ms 3380 KiB
01_random-small_15.txt AC 12 ms 3456 KiB
01_random-small_16.txt AC 12 ms 3452 KiB
01_random-small_17.txt AC 12 ms 3532 KiB
01_random-small_18.txt AC 1 ms 3360 KiB
01_random-small_19.txt AC 12 ms 3508 KiB
01_random-small_20.txt AC 3 ms 3476 KiB
02_handmade-small_01.txt AC 8 ms 3508 KiB
02_handmade-small_02.txt AC 12 ms 3456 KiB
02_handmade-small_03.txt AC 11 ms 3516 KiB
02_handmade-small_04.txt AC 13 ms 3512 KiB
03_random-large_01.txt AC 894 ms 4576 KiB
03_random-large_02.txt AC 596 ms 4248 KiB
03_random-large_03.txt AC 886 ms 4596 KiB
03_random-large_04.txt AC 837 ms 4564 KiB
03_random-large_05.txt AC 870 ms 4612 KiB
03_random-large_06.txt AC 195 ms 3888 KiB
03_random-large_07.txt AC 868 ms 4612 KiB
03_random-large_08.txt AC 465 ms 3876 KiB
03_random-large_09.txt AC 890 ms 4704 KiB
03_random-large_10.txt AC 829 ms 4736 KiB
03_random-large_11.txt AC 887 ms 4520 KiB
03_random-large_12.txt AC 205 ms 3884 KiB
03_random-large_13.txt AC 871 ms 4668 KiB
03_random-large_14.txt AC 623 ms 4200 KiB
03_random-large_15.txt AC 878 ms 4680 KiB
03_random-large_16.txt AC 119 ms 3692 KiB
03_random-large_17.txt AC 822 ms 4520 KiB
03_random-large_18.txt AC 591 ms 4172 KiB
03_random-large_19.txt AC 822 ms 4672 KiB
03_random-large_20.txt AC 657 ms 4296 KiB
04_handmade-large_01.txt AC 538 ms 4696 KiB
04_handmade-large_02.txt AC 870 ms 4736 KiB
04_handmade-large_03.txt AC 809 ms 4628 KiB
04_handmade-large_04.txt AC 900 ms 4672 KiB