結果

問題 No.705 ゴミ拾い Hard
ユーザー ForestedForested
提出日時 2024-03-20 23:16:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 1,500 ms
コード長 2,923 bytes
コンパイル時間 2,374 ms
コンパイル使用メモリ 210,356 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2024-03-20 23:16:09
合計ジャッジ時間 8,079 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 2 ms
6,548 KB
testcase_15 AC 2 ms
6,548 KB
testcase_16 AC 2 ms
6,548 KB
testcase_17 AC 2 ms
6,548 KB
testcase_18 AC 2 ms
6,548 KB
testcase_19 AC 2 ms
6,548 KB
testcase_20 AC 3 ms
6,548 KB
testcase_21 AC 2 ms
6,548 KB
testcase_22 AC 2 ms
6,548 KB
testcase_23 AC 2 ms
6,548 KB
testcase_24 AC 234 ms
12,800 KB
testcase_25 AC 231 ms
12,800 KB
testcase_26 AC 232 ms
12,800 KB
testcase_27 AC 235 ms
12,800 KB
testcase_28 AC 231 ms
12,800 KB
testcase_29 AC 231 ms
12,800 KB
testcase_30 AC 216 ms
12,800 KB
testcase_31 AC 178 ms
12,800 KB
testcase_32 AC 180 ms
12,800 KB
testcase_33 AC 102 ms
12,800 KB
testcase_34 AC 101 ms
12,800 KB
testcase_35 AC 160 ms
12,800 KB
testcase_36 AC 170 ms
12,800 KB
testcase_37 AC 160 ms
12,800 KB
testcase_38 AC 169 ms
12,800 KB
testcase_39 AC 209 ms
12,800 KB
testcase_40 AC 2 ms
6,548 KB
testcase_41 AC 2 ms
6,548 KB
testcase_42 AC 2 ms
6,548 KB
testcase_43 AC 181 ms
12,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using i32 = int;
using i64 = long long;
using i128 = __int128_t;
using u32 = unsigned;
using u64 = unsigned long long;
using u128 = __uint128_t;
using pi = pair<i32, i32>;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = vector<V<T>>;
template <typename T>
using VVV = vector<VV<T>>;
template <typename T>
using PQR = priority_queue<T, V<T>, greater<>>;

#define OVERRIDE4(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, l, r) for (i32 i = (i32)(l); i < (i32)(r); ++i)
#define REP(...) OVERRIDE4(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, l, r) for (i32 i = (i32)(r)-1; i >= (i32)(l); --i)
#define PER(...) OVERRIDE4(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define LEN(x) (i32) x.size()

template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

template <typename T>
i32 lob(const V<T> &arr, T x) {
    return (i32)(lower_bound(ALL(arr), x) - arr.begin());
}
template <typename T>
i32 upb(const V<T> &arr, T x) {
    return (i32)(upper_bound(ALL(arr), x) - arr.begin());
}

// -----------------------------------

constexpr i64 INF64 = 3003003003003003003;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    i32 n;
    cin >> n;
    V<i64> a(n), x(n), y(n);
    REP(i, n) {
        cin >> a[i];
    }
    REP(i, n) {
        cin >> x[i];
    }
    REP(i, n) {
        cin >> y[i];
    }
    auto cost = [&](i32 l, i32 r) -> i64 {
        i64 dx = abs(a[r - 1] - x[l]);
        i64 dy = y[l];
        return dx * dx * dx + dy * dy * dy;
    };
    // cost(0, i) < INF64 なので、dp[i] < INF64
    V<i64> dp(n + 1, INF64);
    dp[0] = 0;
    // i < j
    auto cross = [&](i32 i, i32 j) -> i32 {
        i32 ok = j, ng = n + 1;
        while (ng - ok > 1) {
            i32 mid = (ok + ng) / 2;
            if (dp[i] + cost(i, mid) < dp[j] + cost(j, mid)) {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        return ng;
    };
    deque<pair<i32, i32>> deq;
    deq.emplace_back(0, n + 1);
    REP(i, 1, n + 1) {
        while (deq.front().second <= i) {
            deq.pop_front();
        }
        dp[i] = dp[deq.front().first] + cost(deq.front().first, i);
        while (LEN(deq) >= 2) {
            i32 tmp = cross(deq.back().first, i);
            if (deq[LEN(deq) - 2].second >= tmp) {
                deq.pop_back();
            } else {
                break;
            }
        }
        deq.back().second = cross(deq.back().first, i);
        deq.emplace_back(i, n + 1);
    }
    cout << dp[n] << '\n';
}
0