提出 #53788299


ソースコード 拡げる

#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);
    dist[0] = 0;
    std::vector<int> amin(N);

    auto update = [&](int i, int k) {
        if (i <= k) return;
        auto nd = dist[k] + cost(k, i);
        if (nd < dist[i]) {
            dist[i] = nd;
            amin[i] = k;
        }
    };

    auto dfs = [&](auto& dfs, int l, int r) -> void {
        if (r - l == 1) return;
        int m = (l + r) / 2;
        for (int k = amin[l]; k <= amin[r]; ++k) update(m, k);
        dfs(dfs, l, m);
        for (int k = l + 1; k <= m; ++k) update(r, k);
        dfs(dfs, m, r);
    };

    dfs(dfs, 0, N - 1);
    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;
}

提出情報

提出日時
問題 G - Baseball
ユーザ sotanishy
言語 C++ 20 (gcc 12.2)
得点 650
コード長 3068 Byte
結果 AC
実行時間 311 ms
メモリ 5000 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 650 / 650
結果
AC × 4
AC × 52
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
00_sample_01.txt AC 1 ms 3468 KiB
00_sample_02.txt AC 1 ms 3432 KiB
00_sample_03.txt AC 1 ms 3324 KiB
00_sample_04.txt AC 1 ms 3392 KiB
01_random-small_01.txt AC 5 ms 3424 KiB
01_random-small_02.txt AC 3 ms 3500 KiB
01_random-small_03.txt AC 5 ms 3440 KiB
01_random-small_04.txt AC 2 ms 3412 KiB
01_random-small_05.txt AC 4 ms 3528 KiB
01_random-small_06.txt AC 3 ms 3448 KiB
01_random-small_07.txt AC 5 ms 3488 KiB
01_random-small_08.txt AC 4 ms 3452 KiB
01_random-small_09.txt AC 4 ms 3556 KiB
01_random-small_10.txt AC 1 ms 3476 KiB
01_random-small_11.txt AC 4 ms 3428 KiB
01_random-small_12.txt AC 3 ms 3472 KiB
01_random-small_13.txt AC 4 ms 3512 KiB
01_random-small_14.txt AC 2 ms 3440 KiB
01_random-small_15.txt AC 4 ms 3504 KiB
01_random-small_16.txt AC 4 ms 3560 KiB
01_random-small_17.txt AC 4 ms 3560 KiB
01_random-small_18.txt AC 1 ms 3476 KiB
01_random-small_19.txt AC 5 ms 3500 KiB
01_random-small_20.txt AC 1 ms 3452 KiB
02_handmade-small_01.txt AC 3 ms 3460 KiB
02_handmade-small_02.txt AC 4 ms 3524 KiB
02_handmade-small_03.txt AC 5 ms 3504 KiB
02_handmade-small_04.txt AC 3 ms 3564 KiB
03_random-large_01.txt AC 278 ms 5000 KiB
03_random-large_02.txt AC 190 ms 4308 KiB
03_random-large_03.txt AC 311 ms 4856 KiB
03_random-large_04.txt AC 272 ms 4948 KiB
03_random-large_05.txt AC 276 ms 4892 KiB
03_random-large_06.txt AC 66 ms 3608 KiB
03_random-large_07.txt AC 276 ms 4852 KiB
03_random-large_08.txt AC 156 ms 4184 KiB
03_random-large_09.txt AC 244 ms 4908 KiB
03_random-large_10.txt AC 263 ms 4884 KiB
03_random-large_11.txt AC 250 ms 4904 KiB
03_random-large_12.txt AC 70 ms 3660 KiB
03_random-large_13.txt AC 276 ms 4868 KiB
03_random-large_14.txt AC 190 ms 4316 KiB
03_random-large_15.txt AC 299 ms 4852 KiB
03_random-large_16.txt AC 40 ms 3528 KiB
03_random-large_17.txt AC 259 ms 4772 KiB
03_random-large_18.txt AC 214 ms 4372 KiB
03_random-large_19.txt AC 264 ms 4840 KiB
03_random-large_20.txt AC 187 ms 4340 KiB
04_handmade-large_01.txt AC 132 ms 4936 KiB
04_handmade-large_02.txt AC 240 ms 4772 KiB
04_handmade-large_03.txt AC 271 ms 4928 KiB
04_handmade-large_04.txt AC 136 ms 4800 KiB