結果

問題 No.704 ゴミ拾い Medium
ユーザー yosupotyosupot
提出日時 2018-06-15 22:42:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 152 ms / 1,500 ms
コード長 1,696 bytes
コンパイル時間 3,446 ms
コンパイル使用メモリ 204,852 KB
実行使用メモリ 14,852 KB
最終ジャッジ日時 2023-09-13 04:59:26
合計ジャッジ時間 11,913 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 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,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 152 ms
14,668 KB
testcase_25 AC 152 ms
14,732 KB
testcase_26 AC 152 ms
14,724 KB
testcase_27 AC 150 ms
14,716 KB
testcase_28 AC 151 ms
14,700 KB
testcase_29 AC 151 ms
14,732 KB
testcase_30 AC 152 ms
14,672 KB
testcase_31 AC 151 ms
14,652 KB
testcase_32 AC 151 ms
14,652 KB
testcase_33 AC 150 ms
14,768 KB
testcase_34 AC 136 ms
14,684 KB
testcase_35 AC 136 ms
14,612 KB
testcase_36 AC 136 ms
14,852 KB
testcase_37 AC 137 ms
14,648 KB
testcase_38 AC 136 ms
14,656 KB
testcase_39 AC 135 ms
14,688 KB
testcase_40 AC 136 ms
14,708 KB
testcase_41 AC 137 ms
14,680 KB
testcase_42 AC 135 ms
14,712 KB
testcase_43 AC 137 ms
14,704 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 148 ms
14,644 KB
testcase_47 AC 148 ms
14,752 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;
    ll x = abs(a.first - b.first);
    ll y = abs(a.second - b.second);
    return x + 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