結果

問題 No.705 ゴミ拾い Hard
ユーザー antaanta
提出日時 2018-06-16 01:03:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 195 ms / 1,500 ms
コード長 1,984 bytes
コンパイル時間 1,845 ms
コンパイル使用メモリ 176,188 KB
実行使用メモリ 12,704 KB
最終ジャッジ日時 2023-09-13 05:40:00
合計ジャッジ時間 7,372 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 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,380 KB
testcase_18 AC 2 ms
4,376 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 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 194 ms
12,636 KB
testcase_25 AC 195 ms
12,548 KB
testcase_26 AC 194 ms
12,668 KB
testcase_27 AC 195 ms
12,600 KB
testcase_28 AC 194 ms
12,292 KB
testcase_29 AC 195 ms
12,660 KB
testcase_30 AC 193 ms
12,580 KB
testcase_31 AC 185 ms
12,560 KB
testcase_32 AC 188 ms
12,624 KB
testcase_33 AC 162 ms
12,540 KB
testcase_34 AC 163 ms
12,472 KB
testcase_35 AC 175 ms
12,672 KB
testcase_36 AC 186 ms
12,704 KB
testcase_37 AC 173 ms
12,516 KB
testcase_38 AC 183 ms
12,248 KB
testcase_39 AC 187 ms
12,424 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 195 ms
12,556 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 solveOffline = [monotoneMinima](const long long *prev, int m, int n, auto f, auto out) {
			monotoneMinima(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