結果

問題 No.1467 Selling Cars
ユーザー QCFiumQCFium
提出日時 2021-03-12 11:42:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,077 bytes
コンパイル時間 1,976 ms
コンパイル使用メモリ 175,764 KB
実行使用メモリ 35,692 KB
最終ジャッジ日時 2024-04-21 22:15:19
合計ジャッジ時間 12,872 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int64_t fast_one(const std::vector<int64_t> &a, const std::vector<int64_t> &b) {
	int n = a.size();
	int m = b.size();
	
	int used[n + 1];
	for (auto &i : used) i = -1;
	
	// std::cerr << "start" << std::endl;
	int closest = 0;
	for (int i = 0; i < m; i++) {
		// std::cerr << i << ":" << b[i] << std::endl;
		while (closest + 1 < n && std::abs(b[i] - a[closest]) >= std::abs(b[i] - a[closest + 1])) closest++;
		if (used[closest] != -1) {
			int r = closest;
			int l = closest;
			while (l && used[l - 1] != -1) l--;
			while (r + 1 < n && used[r + 1] != -1) r++;
			r++;
			used[r] = i;
			// std::cerr << "conflict [" << l << ", " << r << ")" << std::endl;
			
			int64_t shift_delta = 0;
			for (int j = l; j <= r; j++) shift_delta += std::abs(a[j - 1] - b[used[j]]) - std::abs(a[j] - b[used[j]]);
			if (shift_delta < 0) {
				// std::cerr << " shift" << std::endl;
				for (int j = l; j <= r; j++) {
					used[j - 1] = used[j];
					used[j] = -1;
				}
			}
		} else used[closest] = i;
	}
	int64_t res = 0;
	for (int i = 0; i < n; i++) if (used[i] != -1) res += std::abs(a[i] - b[used[i]]);
	return res;
}

std::vector<int64_t> fast(std::vector<int> a_, std::vector<int> b_) {
	std::sort(a_.begin(), a_.end());
	std::sort(b_.begin(), b_.end());
	
	std::vector<int64_t> a(a_.begin(), a_.end());
	std::vector<int64_t> b(b_.begin(), b_.end());
	int n = a.size();
	int m = b.size();
	a.insert(a.begin(), -1000000000000000000);
	a.insert(a.end(), 1000000000000000000);
	b.insert(b.begin(), -1000000000000000000);
	b.insert(b.end(), 1000000000000000000);
	std::vector<int64_t> res;
	for (int i = 1; i <= m; i++) {
		std::vector<int64_t> aa;
		for (auto j : a) for (int k = 0; k < i; k++) aa.push_back(j);
		res.push_back(fast_one(aa, b));
	}
	
	return res;
}


int main() {
	int m = ri();
	int n = ri();
	std::vector<int> a(m);
	std::vector<int> b(n);
	for (auto &i : a) i = ri();
	for (auto &i : b) i = ri();
	
	auto res = fast(b, a);
	for (auto i : res) printf("%" PRId64 "\n", i);
	
	return 0;
}
0