結果

問題 No.1467 Selling Cars
ユーザー QCFium
提出日時 2021-03-12 11:57:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,754 ms / 4,000 ms
コード長 1,134 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 270,336 KB
最終ジャッジ日時 2024-10-14 07:52:00
合計ジャッジ時間 26,621 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

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