結果

問題 No.705 ゴミ拾い Hard
ユーザー yosupotyosupot
提出日時 2018-06-15 22:42:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 194 ms / 1,500 ms
コード長 1,597 bytes
コンパイル時間 3,328 ms
コンパイル使用メモリ 205,320 KB
実行使用メモリ 14,796 KB
最終ジャッジ日時 2023-09-13 04:58:24
合計ジャッジ時間 10,390 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 185 ms
14,760 KB
testcase_25 AC 183 ms
14,796 KB
testcase_26 AC 183 ms
14,716 KB
testcase_27 AC 185 ms
14,720 KB
testcase_28 AC 182 ms
14,720 KB
testcase_29 AC 183 ms
14,708 KB
testcase_30 AC 184 ms
14,732 KB
testcase_31 AC 194 ms
14,708 KB
testcase_32 AC 182 ms
14,724 KB
testcase_33 AC 146 ms
14,616 KB
testcase_34 AC 145 ms
14,668 KB
testcase_35 AC 156 ms
14,796 KB
testcase_36 AC 174 ms
14,732 KB
testcase_37 AC 156 ms
14,672 KB
testcase_38 AC 174 ms
14,704 KB
testcase_39 AC 178 ms
14,624 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 179 ms
14,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); }
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

const ll INF = 8 * TEN(18);

using P = pair<ll, ll>;

ll dist(const P &a, const P &b) {
    ll x = abs(a.first - b.first);
    ll y = abs(a.second - b.second);
    return x*x*x + y*y*y;
}

int n;
V<P> s, t;
V<ll> dp;

void solve2(int l, int r, int a, int b) {
    if (l == r) return;
    int md = (l+r)/2;
    ll ma = INF; int mi = -1;
    for (int i = a; i < b; i++) {
        ll cst = dp[i+1] + dist(s[i], t[md]);
        if (cst < ma) {
            ma = cst;
            mi = i;
        }
    }
    dp[md] = min(dp[md], ma);
    solve2(l, md, a, mi+1);
    solve2(md+1, r, mi, b);
}

void solve(int l, int r) {
    if (r-l == 1) {
        dp[l] = min(dp[l], dist(s[l], t[l]) + dp[r]);
        return;
    }
    int md = (l+r)/2;
    solve(md, r);
    solve2(l, md, md, r);
    solve(l, md);
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20) << fixed;

    cin >> n;
    s = V<P>(n); t = V<P>(n); dp = V<ll>(n+1, INF); dp[n] = 0;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        s[i] = P(a, 0);
    }
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        t[i].first = a;
    }
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        t[i].second = a;
    }

    solve(0, n);
    cout << dp[0] << endl;
    return 0;
}
0