結果

問題 No.1868 Teleporting Cyanmond
ユーザー horitaka1999horitaka1999
提出日時 2022-03-11 22:04:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 445 ms / 2,000 ms
コード長 2,187 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 86,880 KB
実行使用メモリ 97,696 KB
最終ジャッジ日時 2023-10-14 07:13:57
合計ジャッジ時間 10,895 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,340 KB
testcase_01 AC 78 ms
71,228 KB
testcase_02 AC 80 ms
71,564 KB
testcase_03 AC 409 ms
95,260 KB
testcase_04 AC 350 ms
88,408 KB
testcase_05 AC 190 ms
79,036 KB
testcase_06 AC 309 ms
82,108 KB
testcase_07 AC 367 ms
84,848 KB
testcase_08 AC 331 ms
82,216 KB
testcase_09 AC 445 ms
88,040 KB
testcase_10 AC 434 ms
93,316 KB
testcase_11 AC 132 ms
77,916 KB
testcase_12 AC 354 ms
83,556 KB
testcase_13 AC 356 ms
84,880 KB
testcase_14 AC 416 ms
95,576 KB
testcase_15 AC 381 ms
88,364 KB
testcase_16 AC 259 ms
81,664 KB
testcase_17 AC 298 ms
82,012 KB
testcase_18 AC 410 ms
97,528 KB
testcase_19 AC 410 ms
97,468 KB
testcase_20 AC 410 ms
97,316 KB
testcase_21 AC 405 ms
97,436 KB
testcase_22 AC 411 ms
97,696 KB
testcase_23 AC 363 ms
97,484 KB
testcase_24 AC 349 ms
97,400 KB
testcase_25 AC 361 ms
97,536 KB
testcase_26 AC 368 ms
97,324 KB
testcase_27 AC 364 ms
97,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res
    def value(self,index):
        return self.query(index,index+1)

N = int(input())
R = [0] + list(map(int,input().split()))
dp = [0 for _ in range(N+1)]
dp[1] = 0
INF = float('inf')
tmp = [INF for _ in range(N+1)]
seg = SegTree(tmp,segfunc=lambda x,y:min(x,y),ide_ele=INF)
seg.update(R[1],dp[1])
for i in range(2,N+1):
    dp[i] = seg.query(i,N+1) + 1
    if i == N:
        continue
    seg.update(R[i],min(seg.value(R[i]),dp[i]))
print(dp[N])
0