結果

問題 No.705 ゴミ拾い Hard
ユーザー antaanta
提出日時 2018-06-16 02:09:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 287 ms / 1,500 ms
コード長 3,058 bytes
コンパイル時間 1,855 ms
コンパイル使用メモリ 177,104 KB
実行使用メモリ 14,676 KB
最終ジャッジ日時 2023-09-13 05:45:41
合計ジャッジ時間 9,020 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 15 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 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,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 281 ms
14,464 KB
testcase_25 AC 280 ms
14,640 KB
testcase_26 AC 280 ms
14,624 KB
testcase_27 AC 281 ms
14,516 KB
testcase_28 AC 279 ms
14,516 KB
testcase_29 AC 281 ms
14,616 KB
testcase_30 AC 287 ms
14,592 KB
testcase_31 AC 270 ms
14,668 KB
testcase_32 AC 275 ms
14,676 KB
testcase_33 AC 193 ms
14,600 KB
testcase_34 AC 194 ms
14,560 KB
testcase_35 AC 258 ms
14,516 KB
testcase_36 AC 271 ms
14,660 KB
testcase_37 AC 258 ms
14,596 KB
testcase_38 AC 276 ms
14,524 KB
testcase_39 AC 271 ms
14,488 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 255 ms
14,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);
	}
}
0