結果

問題 No.992 最長増加部分列の数え上げ
ユーザー maspymaspy
提出日時 2020-03-25 14:05:58
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,268 bytes
コンパイル時間 315 ms
コンパイル使用メモリ 11,172 KB
実行使用メモリ 70,504 KB
最終ジャッジ日時 2023-08-30 09:38:45
合計ジャッジ時間 48,932 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,432 KB
testcase_01 AC 19 ms
8,484 KB
testcase_02 AC 18 ms
8,568 KB
testcase_03 AC 18 ms
8,568 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from bisect import bisect_left
import itertools

INF = 2 * 10 ** 9 + 10
N = int(readline())
A = (0,) + tuple(int(x) + 10 ** 9 + 1 for x in read().split())
LIS = [0] * (N + 1)
dp = [INF] * (N + 1)
dp[0] = 0
for n, x in enumerate(A[1:], 1):
    m = bisect_left(dp, x)
    dp[m] = x
    LIS[n] = m


class BinaryIndexedTree():
    def __init__(self, seq):
        self.size = len(seq)
        self.depth = self.size.bit_length()
        self.build(seq)

    def build(self, seq):
        data = seq
        size = self.size
        for i, x in enumerate(data):
            j = i + (i & (-i))
            if j < size:
                data[j] += data[i]
        self.data = data

    def __repr__(self):
        return self.data.__repr__()

    def get_sum(self, i):
        data = self.data
        s = 0
        while i:
            s += data[i]
            i -= i & -i
        return s

    def add(self, i, x):
        data = self.data
        size = self.size
        while i < size:
            data[i] += x
            i += i & -i

    def find_kth_element(self, k):
        data = self.data
        size = self.size
        x, sx = 0, 0
        dx = 1 << (self.depth)
        for i in range(self.depth - 1, -1, -1):
            dx = (1 << i)
            if x + dx >= size:
                continue
            y = x + dx
            sy = sx + data[y]
            if sy < k:
                x, sx = y, sy
        return x + 1


M_LIS = max(LIS)
LIS_cnt = [0] * (M_LIS + 1)
for i, x in enumerate(LIS):
    LIS_cnt[x] += 1
LIS_cnt = tuple(itertools.accumulate(LIS_cnt))

keys = [(x << 32) + y for x, y in zip(LIS, A)]
s_keys = sorted(keys)
key_rank = {x: i for i, x in enumerate(s_keys)}

bit = BinaryIndexedTree([0] * (N + 1))  # key の値に対する数え上げを管理

answer = 0
for L, k, a in zip(LIS[1:], keys[1:], A[1:]):
    if L == 1:
        x = 1
    else:
        r_key = k - (1 << 32)
        right = bisect_left(s_keys, r_key) - 1
        left = LIS_cnt[L - 2]
        x = bit.get_sum(right) - bit.get_sum(left - 1)
    if L == M_LIS:
        answer += x
    else:
        bit.add(key_rank[k], x)

print(answer)
0