結果

問題 No.1467 Selling Cars
ユーザー QCFiumQCFium
提出日時 2021-03-12 11:57:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,942 ms / 4,000 ms
コード長 1,134 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 270,392 KB
最終ジャッジ日時 2024-04-22 08:38:52
合計ジャッジ時間 29,612 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,942 ms
270,376 KB
testcase_01 AC 1,448 ms
269,056 KB
testcase_02 AC 43 ms
54,272 KB
testcase_03 AC 1,127 ms
235,840 KB
testcase_04 AC 1,163 ms
236,880 KB
testcase_05 AC 1,136 ms
236,072 KB
testcase_06 AC 1,144 ms
236,160 KB
testcase_07 AC 1,143 ms
237,056 KB
testcase_08 AC 1,128 ms
236,396 KB
testcase_09 AC 49 ms
63,232 KB
testcase_10 AC 49 ms
63,232 KB
testcase_11 AC 713 ms
164,052 KB
testcase_12 AC 917 ms
200,868 KB
testcase_13 AC 809 ms
184,704 KB
testcase_14 AC 174 ms
86,480 KB
testcase_15 AC 341 ms
126,216 KB
testcase_16 AC 281 ms
97,964 KB
testcase_17 AC 678 ms
152,820 KB
testcase_18 AC 841 ms
184,576 KB
testcase_19 AC 1,794 ms
270,392 KB
testcase_20 AC 1,114 ms
244,204 KB
testcase_21 AC 1,008 ms
236,416 KB
testcase_22 AC 1,745 ms
267,904 KB
testcase_23 AC 1,270 ms
264,576 KB
testcase_24 AC 1,112 ms
248,448 KB
testcase_25 AC 147 ms
83,540 KB
testcase_26 AC 514 ms
150,144 KB
testcase_27 AC 416 ms
118,852 KB
testcase_28 AC 999 ms
186,748 KB
testcase_29 AC 220 ms
95,232 KB
testcase_30 AC 209 ms
90,708 KB
testcase_31 AC 43 ms
53,760 KB
testcase_32 AC 43 ms
53,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections

def fast_one(a, b, k) :
	n = len(a)
	m = len(b)
	
	used = [collections.deque() for i in range(n + 1)]
	
	closest = 0
	for i in range(m) :
		while closest + 1 < n and abs(b[i] - a[closest]) >= abs(b[i] - a[closest + 1]) : closest += 1
		if len(used[closest]) == k :
			r = closest
			l = closest
			while l and len(used[l - 1]) == k : l -= 1
			while r + 1 < n and len(used[r + 1]) == k : r += 1
			r += 1
			used[r].append(i)
			
			shift_delta = 0
			for j in range(l, r + 1) : shift_delta += abs(a[j - 1] - b[used[j][0]]) - abs(a[j] - b[used[j][0]])
			if shift_delta < 0 :
				for j in range(l, r + 1) :
					used[j - 1].append(used[j][0])
					used[j].popleft()
		else : used[closest].append(i)
	
	res = 0
	for i in range(n) :
		for j in used[i] : res += abs(a[i] - b[j])
	
	return res

def fast(a, b) :
	a.sort()
	b.sort()
	a = [-1000000000000000000] + a + [1000000000000000000]
	
	res = []
	for i in range(1, m + 1) : res.append(fast_one(a, b, i))
	return res

m, n = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

res = fast(b, a)
for i in res : print(i)
0