JOI 2009 本選 2 – ピザ

AtCoderなど過去問

JOI 2009 本選 2 – ピザ

d = int(input())
n = int(input())
m = int(input())

shop = [int(input()) for _ in range(n-1)]
cus = [int(input()) for _ in range(m)]
shop.append(0)
shop.append(d)
shop.sort()
cus.sort()

def solve(x):
    l = 0
    r = len(shop)
    while l <= r:
        mid = (l + r)//2
        if shop[mid] == x:
            return 0
        if shop[mid] < x:
            l = mid + 1
        else:
            r = mid -1
    if shop[mid] < x:
        return min(abs(shop[mid] - x), abs(shop[mid+1] - x))
    else:
        return min(abs(shop[mid] - x), abs(shop[mid-1] - x))


ans = 0
for x in cus:
    ans += solve(x)

print(ans)

コメント

タイトルとURLをコピーしました