結果

問題 No.1467 Selling Cars
ユーザー QCFiumQCFium
提出日時 2021-03-12 11:34:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 558 ms / 4,000 ms
コード長 1,859 bytes
コンパイル時間 2,086 ms
コンパイル使用メモリ 179,168 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 08:35:54
合計ジャッジ時間 13,423 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 508 ms
5,248 KB
testcase_01 AC 558 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 479 ms
5,376 KB
testcase_04 AC 478 ms
5,376 KB
testcase_05 AC 486 ms
5,376 KB
testcase_06 AC 483 ms
5,376 KB
testcase_07 AC 481 ms
5,376 KB
testcase_08 AC 477 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 268 ms
5,376 KB
testcase_12 AC 326 ms
5,376 KB
testcase_13 AC 315 ms
5,376 KB
testcase_14 AC 55 ms
5,376 KB
testcase_15 AC 114 ms
5,376 KB
testcase_16 AC 85 ms
5,376 KB
testcase_17 AC 279 ms
5,376 KB
testcase_18 AC 353 ms
5,376 KB
testcase_19 AC 556 ms
5,376 KB
testcase_20 AC 481 ms
5,376 KB
testcase_21 AC 415 ms
5,376 KB
testcase_22 AC 552 ms
5,376 KB
testcase_23 AC 502 ms
5,376 KB
testcase_24 AC 481 ms
5,376 KB
testcase_25 AC 22 ms
5,376 KB
testcase_26 AC 190 ms
5,376 KB
testcase_27 AC 164 ms
5,376 KB
testcase_28 AC 278 ms
5,376 KB
testcase_29 AC 58 ms
5,376 KB
testcase_30 AC 60 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 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);
	b.insert(b.begin(), -1000000000000000000);
	b.insert(b.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