結果
問題 | No.705 ゴミ拾い Hard |
ユーザー | anta |
提出日時 | 2018-06-16 02:09:09 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 281 ms / 1,500 ms |
コード長 | 3,058 bytes |
コンパイル時間 | 1,943 ms |
コンパイル使用メモリ | 180,740 KB |
実行使用メモリ | 14,912 KB |
最終ジャッジ日時 | 2024-06-30 15:55:48 |
合計ジャッジ時間 | 8,910 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,948 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,944 KB |
testcase_10 | AC | 1 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,944 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 2 ms
6,940 KB |
testcase_14 | AC | 3 ms
6,940 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 2 ms
6,940 KB |
testcase_17 | AC | 3 ms
6,944 KB |
testcase_18 | AC | 3 ms
6,940 KB |
testcase_19 | AC | 2 ms
6,944 KB |
testcase_20 | AC | 3 ms
6,944 KB |
testcase_21 | AC | 2 ms
6,940 KB |
testcase_22 | AC | 2 ms
6,944 KB |
testcase_23 | AC | 2 ms
6,940 KB |
testcase_24 | AC | 268 ms
14,872 KB |
testcase_25 | AC | 278 ms
14,872 KB |
testcase_26 | AC | 281 ms
14,868 KB |
testcase_27 | AC | 276 ms
14,740 KB |
testcase_28 | AC | 278 ms
14,868 KB |
testcase_29 | AC | 278 ms
14,868 KB |
testcase_30 | AC | 280 ms
14,740 KB |
testcase_31 | AC | 262 ms
14,752 KB |
testcase_32 | AC | 265 ms
14,784 KB |
testcase_33 | AC | 192 ms
14,744 KB |
testcase_34 | AC | 195 ms
14,848 KB |
testcase_35 | AC | 252 ms
14,756 KB |
testcase_36 | AC | 263 ms
14,784 KB |
testcase_37 | AC | 252 ms
14,756 KB |
testcase_38 | AC | 263 ms
14,912 KB |
testcase_39 | AC | 260 ms
14,740 KB |
testcase_40 | AC | 2 ms
6,944 KB |
testcase_41 | AC | 2 ms
6,940 KB |
testcase_42 | AC | 1 ms
6,944 KB |
testcase_43 | AC | 242 ms
14,740 KB |
ソースコード
#include "bits/stdc++.h" using namespace std; template<typename T, typename U> static void amin(T &x, U y) { if (y < x) x = y; } const long long INFL = 0x3f3f3f3f3f3f3f3fLL; int main() { int N; while (~scanf("%d", &N)) { vector<int> as(N); for (int i = 0; i < N; ++ i) scanf("%d", &as[i]); vector<int> xs(N); for (int i = 0; i < N; ++ i) scanf("%d", &xs[i]); vector<int> ys(N); for (int i = 0; i < N; ++ i) scanf("%d", &ys[i]); sort(as.begin(), as.end()); sort(xs.begin(), xs.end()); auto values = xs; sort(values.begin(), values.end()); values.erase(unique(values.begin(), values.end()), values.end()); auto cb = [](int x) { return (long long)x * x * x; }; auto monotoneMinima = [](int n, int m, auto f, auto out) { auto rec = [=](auto rec, int L, int R, int jL, int jR) -> void { if (L > R) return; int mid = (L + R) / 2; auto t = f(mid, jL); int jM = jL; for (int j = jL + 1; j <= jR; ++ j) { auto u = f(mid, j); if (u < t) t = u, jM = j; } out(mid, t, jM); rec(rec, L, mid - 1, jL, jM); rec(rec, mid + 1, R, jM, jR); }; rec(rec, 0, n - 1, 0, m - 1); }; auto totallyMonotoneMinima = [](int N, int M, auto f, auto out) { vector<int> minima(N, -1); auto rec = [N, f, out, &minima](auto rec, int k, int L, const vector<int> &js) -> void { if (L >= N) return; int s = 1 << k, n = ((N - L - 1) >> k) + 1; vector<int> njs; for (int j : js) { int i = L + ((int)njs.size() << k); while (!njs.empty() && !(f(i, njs.back()) < f(i, j))) njs.pop_back(), i -= s; if ((int)njs.size() + 1 < n) njs.push_back(j); } rec(rec, k + 1, L + s, njs); for (int i = L, jx = 0; i < N; i += s * 2) { int jR = i + s < N ? minima[i + s] : js.back(); auto t = f(i, js[jx]); int minj = js[jx]; for (++ jx; jx != js.size() && js[jx] <= jR; ++ jx) { auto u = f(i, js[jx]); if (u < t) t = u, minj = js[jx]; } minima[i] = minj; out(i, t, minj); if (js[jx - 1] == jR) -- jx; } }; vector<int> js; for (int j = 0; j < M; ++ j) js.push_back(j); rec(rec, 0, 0, js); assert(count(minima.begin(), minima.end(), -1) == 0); }; auto solveOffline = [totallyMonotoneMinima](const long long *prev, int m, int n, auto f, auto out) { totallyMonotoneMinima(n, m, [=](int i, int j) { return prev[j] + f(j, i); }, [=](int i, long long t, int minj) { out(i, t); }); }; vector<long long> dp(N + 1, INFL); dp[0] = 0; vector<long long> tmp(N + 1); function<void(int, int)> rec = [&](int L, int R) { if (R - L <= 1) return; int mid = (L + R) / 2; rec(L, mid); for (int i = 0; i < R - L; ++ i) tmp[i] = dp[L + i] + (L + i == N ? 0 : cb(ys[L + i])); solveOffline(tmp.data(), mid - L, R - mid, [&](int j, int i) { j += L, i += mid; return cb(abs(as[i - 1] - xs[j])); }, [&](int i, long long t) { i += mid; amin(dp[i], t); }); rec(mid, R); }; rec(0, N + 1); long long ans = dp[N]; printf("%lld\n", ans); } }