結果

問題 No.1687 What the Heck?
ユーザー brthyyjpbrthyyjp
提出日時 2021-09-24 21:40:28
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,780 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 108,700 KB
最終ジャッジ日時 2023-09-18 20:58:32
合計ジャッジ時間 11,062 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,316 KB
testcase_01 AC 77 ms
71,356 KB
testcase_02 WA -
testcase_03 AC 74 ms
71,584 KB
testcase_04 AC 77 ms
71,356 KB
testcase_05 AC 77 ms
71,080 KB
testcase_06 AC 74 ms
71,316 KB
testcase_07 AC 429 ms
86,480 KB
testcase_08 AC 574 ms
91,780 KB
testcase_09 WA -
testcase_10 AC 305 ms
83,452 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1,132 ms
108,644 KB
testcase_14 AC 1,096 ms
108,456 KB
testcase_15 AC 1,107 ms
108,660 KB
testcase_16 AC 1,085 ms
108,488 KB
testcase_17 AC 147 ms
77,692 KB
testcase_18 AC 1,124 ms
108,700 KB
testcase_19 AC 106 ms
77,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0]*(self.n+1) # 1-indexed

    def init(self, init_val):
        for i, v in enumerate(init_val):
            self.add(i, v)

    def add(self, i, x):
        # i: 0-indexed
        i += 1 # to 1-indexed
        while i <= self.n:
            self.bit[i] += x
            i += (i & -i)

    def sum(self, i, j):
        # return sum of [i, j)
        # i, j: 0-indexed
        return self._sum(j) - self._sum(i)

    def _sum(self, i):
        # return sum of [0, i)
        # i: 0-indexed
        res = 0
        while i > 0:
            res += self.bit[i]
            i -= i & (-i)
        return res

    def lower_bound(self, x):
        s = 0
        pos = 0
        depth = self.n.bit_length()
        v = 1 << depth
        for i in range(depth, -1, -1):
            k = pos + v
            if k <= self.n and s + self.bit[k] < x:
                    s += self.bit[k]
                    pos += v
            v >>= 1
        return pos

    def __str__(self): # for debug
        arr = [self.sum(i,i+1) for i in range(self.n)]
        return str(arr)

n = int(input())
P = list(map(int, input().split()))
bit = BIT(n+5)
for i in range(1, n+1):
    bit.add(i, 1)
ans = 0
for i in reversed(range(n)):
    p = P[i]
    s = bit.sum(p+1, bit.n)
    if s == 0:
        if bit.sum(p, p+1) > 0:
            bit.add(p, -1)
        else:
            j = bit.lower_bound(1)
            if j != p:
                ans -= (i+1)
            bit.add(j, -1)
    else:
        ng = p+1
        ok = bit.n
        while ng+1 < ok:
            c = (ng+ok)//2
            if bit.sum(p+1, c) > 0:
                ok = c
            else:
                ng = c
        ans += i+1
        bit.add(ok-1, -1)
print(ans)
0