結果

問題 No.808 Kaiten Sushi?
ユーザー mkawa2mkawa2
提出日時 2022-03-03 12:21:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 978 ms / 2,000 ms
コード長 3,886 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 254,068 KB
最終ジャッジ日時 2023-09-24 08:26:17
合計ジャッジ時間 25,288 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,364 KB
testcase_01 AC 76 ms
71,336 KB
testcase_02 AC 77 ms
71,360 KB
testcase_03 AC 74 ms
71,508 KB
testcase_04 AC 74 ms
71,104 KB
testcase_05 AC 72 ms
71,360 KB
testcase_06 AC 76 ms
71,524 KB
testcase_07 AC 147 ms
80,152 KB
testcase_08 AC 147 ms
79,748 KB
testcase_09 AC 177 ms
81,016 KB
testcase_10 AC 160 ms
80,416 KB
testcase_11 AC 148 ms
80,372 KB
testcase_12 AC 161 ms
80,504 KB
testcase_13 AC 144 ms
79,700 KB
testcase_14 AC 101 ms
76,684 KB
testcase_15 AC 176 ms
81,152 KB
testcase_16 AC 161 ms
80,752 KB
testcase_17 AC 117 ms
78,432 KB
testcase_18 AC 99 ms
76,304 KB
testcase_19 AC 91 ms
76,032 KB
testcase_20 AC 97 ms
76,296 KB
testcase_21 AC 90 ms
76,128 KB
testcase_22 AC 98 ms
76,208 KB
testcase_23 AC 374 ms
126,156 KB
testcase_24 AC 329 ms
113,488 KB
testcase_25 AC 351 ms
121,768 KB
testcase_26 AC 343 ms
120,060 KB
testcase_27 AC 786 ms
223,612 KB
testcase_28 AC 399 ms
101,828 KB
testcase_29 AC 729 ms
208,828 KB
testcase_30 AC 509 ms
140,508 KB
testcase_31 AC 836 ms
226,468 KB
testcase_32 AC 923 ms
253,764 KB
testcase_33 AC 528 ms
140,160 KB
testcase_34 AC 557 ms
170,852 KB
testcase_35 AC 648 ms
192,324 KB
testcase_36 AC 492 ms
139,892 KB
testcase_37 AC 74 ms
71,608 KB
testcase_38 AC 75 ms
71,376 KB
testcase_39 AC 906 ms
237,344 KB
testcase_40 AC 302 ms
103,444 KB
testcase_41 AC 328 ms
101,792 KB
testcase_42 AC 757 ms
206,864 KB
testcase_43 AC 372 ms
119,908 KB
testcase_44 AC 603 ms
171,152 KB
testcase_45 AC 363 ms
120,080 KB
testcase_46 AC 274 ms
96,716 KB
testcase_47 AC 953 ms
251,008 KB
testcase_48 AC 966 ms
251,108 KB
testcase_49 AC 947 ms
250,912 KB
testcase_50 AC 943 ms
250,896 KB
testcase_51 AC 978 ms
251,068 KB
testcase_52 AC 528 ms
193,228 KB
testcase_53 AC 686 ms
254,068 KB
testcase_54 AC 76 ms
71,428 KB
testcase_55 AC 74 ms
71,508 KB
testcase_56 AC 73 ms
71,320 KB
testcase_57 AC 312 ms
114,924 KB
testcase_58 AC 430 ms
145,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
md = 10**9+7
# md = 998244353

class BitSum:
    def __init__(self, n):
        self.n = n+1
        self.table = [0]*self.n

    def add(self, i, x):
        i += 1
        while i < self.n:
            self.table[i] += x
            i += i & -i

    def sum(self, i):
        i += 1
        res = 0
        while i > 0:
            res += self.table[i]
            i -= i & -i
        return res

    def pos(self, x):
        idx = 0
        for lv in range((self.n-1).bit_length()-1, -1, -1):
            mid = idx+(1 << lv)
            if mid >= self.n: continue
            if self.table[mid] < x:
                x -= self.table[mid]
                idx += 1 << lv
        return idx

# 要素を先読みする代わりに、[0,10**6]でなくとも使える
class Cset:
    def __init__(self, aa):
        aa = set(aa)
        self.dec = tuple(sorted(aa))
        self.enc = {a: i for i, a in enumerate(self.dec)}
        self.mx = len(self.dec)
        self.size = 0
        self.bit = BitSum(self.mx+1)
        self.nums = [False]*(self.mx+1)

    def __repr__(self):
        return "["+" ".join(str(self[i]) for i in range(self.size))+"]"

    def insert(self, a):
        if a not in self.enc: return False
        a = self.enc[a]
        if a < 0 or a > self.mx or self.nums[a]: return False
        self.size += 1
        self.bit.add(a, 1)
        self.nums[a] = True
        return True

    def remove(self, value):
        if value not in self.enc: return False
        value = self.enc[value]
        if value < 0 or value > self.mx or not self.nums[value]: return False
        self.size -= 1
        self.bit.add(value, -1)
        self.nums[value] = False
        return True

    def erase(self, index):
        i = index
        if i >= self.size or i < -self.size: return False
        if i < 0: i = self.size+i+1
        else: i += 1
        a = self.bit.pos(i)
        self.size -= 1
        self.bit.add(a, -1)
        self.nums[a] = False
        return True

    def __getitem__(self, i):
        if i >= self.size or i < -self.size: return -1
        if i < 0: i = self.size+i+1
        else: i += 1
        return self.dec[self.bit.pos(i)]

    def __contains__(self, item):
        return self.nums[self.enc[item]]

    def __len__(self):
        return self.size

    def bisect_left(self, a):
        if a not in self.enc: return -1
        a = self.enc[a]
        if a < 0 or a > self.mx: return -1
        if a == 0: return 0
        return self.bit.sum(a-1)

    def bisect_right(self, a):
        if a not in self.enc: return -1
        a = self.enc[a]
        if a < 0 or a > self.mx: return -1
        return self.bit.sum(a)

n, l = LI()
xx0 = LI()
yy0 = LI()

ans = now = xx0[0]
xx = Cset(xx0+yy0)
yy = Cset(xx0+yy0)
for x in xx0[1:]: xx.insert(x)
for y in yy0: yy.insert(y)

for _ in range(n-1):
    j = yy.bisect_left(now)%len(yy)  # 一番近いお茶
    i = xx.bisect_left(yy[j])%len(xx)  # お茶後の寿司
    x = xx[i]
    j = (yy.bisect_left(x)-1)%len(yy)  # 次の寿司直前のお茶
    y = yy[j]
    ans += (y-now)%l+(x-y)%l
    xx.remove(x)
    yy.remove(y)
    now = x
    # pDB(y, x, ans, xx, yy)

ans += (yy[0]-now)%l

print(ans)
0