結果

問題 No.992 最長増加部分列の数え上げ
ユーザー maspymaspy
提出日時 2020-03-25 14:06:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,974 ms / 2,000 ms
コード長 2,295 bytes
コンパイル時間 797 ms
コンパイル使用メモリ 11,128 KB
実行使用メモリ 67,336 KB
最終ジャッジ日時 2023-08-30 11:36:48
合計ジャッジ時間 47,560 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,396 KB
testcase_01 AC 18 ms
8,508 KB
testcase_02 AC 17 ms
8,496 KB
testcase_03 AC 17 ms
8,436 KB
testcase_04 AC 678 ms
29,144 KB
testcase_05 AC 495 ms
24,252 KB
testcase_06 AC 831 ms
36,196 KB
testcase_07 AC 607 ms
26,908 KB
testcase_08 AC 303 ms
17,944 KB
testcase_09 AC 621 ms
27,604 KB
testcase_10 AC 857 ms
36,056 KB
testcase_11 AC 1,099 ms
42,204 KB
testcase_12 AC 204 ms
15,204 KB
testcase_13 AC 585 ms
26,300 KB
testcase_14 AC 591 ms
26,660 KB
testcase_15 AC 208 ms
15,232 KB
testcase_16 AC 1,762 ms
62,452 KB
testcase_17 AC 309 ms
17,848 KB
testcase_18 AC 578 ms
26,156 KB
testcase_19 AC 1,090 ms
41,752 KB
testcase_20 AC 1,966 ms
67,212 KB
testcase_21 AC 1,960 ms
67,280 KB
testcase_22 AC 1,957 ms
67,212 KB
testcase_23 AC 1,957 ms
67,252 KB
testcase_24 AC 1,950 ms
67,248 KB
testcase_25 AC 1,958 ms
67,276 KB
testcase_26 AC 1,970 ms
67,336 KB
testcase_27 AC 1,956 ms
67,224 KB
testcase_28 AC 1,972 ms
67,220 KB
testcase_29 AC 1,974 ms
67,148 KB
testcase_30 AC 816 ms
56,756 KB
testcase_31 AC 819 ms
56,720 KB
testcase_32 AC 835 ms
56,672 KB
testcase_33 AC 868 ms
56,724 KB
testcase_34 AC 820 ms
56,692 KB
testcase_35 AC 758 ms
56,648 KB
testcase_36 AC 763 ms
56,640 KB
testcase_37 AC 768 ms
56,676 KB
testcase_38 AC 774 ms
56,752 KB
testcase_39 AC 764 ms
56,628 KB
testcase_40 AC 1,055 ms
56,920 KB
testcase_41 AC 1,019 ms
56,976 KB
testcase_42 AC 959 ms
56,860 KB
testcase_43 AC 982 ms
56,868 KB
testcase_44 AC 1,134 ms
56,856 KB
権限があれば一括ダウンロードができます

ソースコード

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
MOD = 10**9 + 7
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 % MOD)

print(answer % MOD)
0