結果

問題 No.1467 Selling Cars
ユーザー QCFiumQCFium
提出日時 2021-04-02 20:37:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 558 ms / 4,000 ms
コード長 1,774 bytes
コンパイル時間 1,998 ms
コンパイル使用メモリ 179,188 KB
実行使用メモリ 5,256 KB
最終ジャッジ日時 2023-08-25 10:36:35
合計ジャッジ時間 14,616 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 512 ms
5,256 KB
testcase_01 AC 558 ms
5,196 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 480 ms
4,900 KB
testcase_04 AC 473 ms
5,032 KB
testcase_05 AC 475 ms
4,900 KB
testcase_06 AC 467 ms
4,972 KB
testcase_07 AC 473 ms
4,972 KB
testcase_08 AC 465 ms
4,976 KB
testcase_09 AC 3 ms
4,912 KB
testcase_10 AC 3 ms
4,932 KB
testcase_11 AC 297 ms
4,796 KB
testcase_12 AC 315 ms
4,964 KB
testcase_13 AC 282 ms
4,792 KB
testcase_14 AC 49 ms
4,376 KB
testcase_15 AC 111 ms
4,840 KB
testcase_16 AC 83 ms
4,380 KB
testcase_17 AC 242 ms
4,716 KB
testcase_18 AC 309 ms
4,936 KB
testcase_19 AC 557 ms
5,052 KB
testcase_20 AC 474 ms
4,920 KB
testcase_21 AC 375 ms
5,044 KB
testcase_22 AC 554 ms
4,976 KB
testcase_23 AC 486 ms
4,992 KB
testcase_24 AC 408 ms
5,032 KB
testcase_25 AC 21 ms
4,392 KB
testcase_26 AC 217 ms
4,872 KB
testcase_27 AC 150 ms
4,556 KB
testcase_28 AC 253 ms
4,936 KB
testcase_29 AC 56 ms
4,496 KB
testcase_30 AC 59 ms
4,424 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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 k) {
	int n = a.size();
	int m = b.size();
	
	std::deque<int> used[n + 1];
	
	int closest = 0;
	for (int i = 0; i < m; i++) {
		while (closest + 1 < n && std::abs(b[i] - a[closest]) >= std::abs(b[i] - a[closest + 1])) closest++;
		if ((int) used[closest].size() == k) {
			int r = closest;
			int l = closest;
			while (l && (int) used[l - 1].size() == k) l--;
			while (r + 1 < n && (int) used[r + 1].size() == k) r++;
			r++;
			used[r].push_back(i);
			
			int64_t shift_delta = 0;
			for (int j = l; j <= r; j++) shift_delta += std::abs(a[j - 1] - b[used[j][0]]) - std::abs(a[j] - b[used[j][0]]);
			if (shift_delta < 0) {
				for (int j = l; j <= r; j++) {
					used[j - 1].push_back(used[j].front());
					used[j].pop_front();
				}
			}
		} else used[closest].push_back(i);
	}
	int64_t res = 0;
	for (int i = 0; i < n; i++) for (auto j : used[i]) res += std::abs(a[i] - b[j]);
	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);
	(void) n;
	
	std::vector<int64_t> res;
	for (int i = 1; i <= m; i++) res.push_back(fast_one(a, b, i));
	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