結果

問題 No.1867 Partitions and Inversions
ユーザー shiomusubi496shiomusubi496
提出日時 2022-03-02 16:21:00
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,192 bytes
コンパイル時間 1,626 ms
コンパイル使用メモリ 86,592 KB
実行使用メモリ 226,372 KB
最終ジャッジ日時 2023-09-23 08:56:25
合計ジャッジ時間 29,982 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
70,964 KB
testcase_01 AC 74 ms
70,940 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 AC 74 ms
71,040 KB
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 RE -
testcase_59 RE -
testcase_60 RE -
testcase_61 RE -
testcase_62 RE -
testcase_63 AC 1,951 ms
220,092 KB
testcase_64 AC 75 ms
71,156 KB
testcase_65 AC 77 ms
70,920 KB
testcase_66 AC 79 ms
71,064 KB
testcase_67 AC 1,882 ms
220,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Manager:
    class node:
        def __init__(self, a, b, c) -> None:
            self.i = a
            self.m = b
            self.limit = c

    def __init__(self, D) -> None:
        self.n = len(D)
        self.C = D
        self.data = []
        assert len(D) + 1 == len(D[0])

    def add(self, i, m) -> None:
        if not self.data:
            self.data.append(self.node(i, m, self.n))
            return
        else:
            while self.data[-1].limit < i + 1:
                self.data.pop()
            d = self.data[-1]
            if self.C[d.i][i + 1] + d.m < self.C[i][i + 1] + m:
                return

        pushed = False
        while self.data:
            d = self.data[-1]
            if self.C[d.i][d.limit] + d.m > self.C[i][d.limit] + m:
                self.data.pop()
            else:
                ok = i + 1
                ng = d.limit
                while ng - ok > 1:
                    mid = (ok + ng) >> 1
                    if self.C[d.i][mid] + d.m > self.C[i][mid] + m:
                        ok = mid
                    else:
                        ng = mid
                self.data.append(self.node(i, m, ok))
                pushed = True
                break

        if not pushed:
            self.data.append((i, m, self.n))

    def get_min(self, x) -> int:
        while self.data[-1].limit < x:
            self.data.pop()
        d = self.data[-1]
        return self.C[d.i][x] + d.m

    def clear_log(self) -> None:
        while self.data:
            self.data.pop()


N = int(input())
P = list(map(int, input().split()))

D = [[0] * (N + 1) for _ in range(N)]
for i in range(N - 1):
    R = [0] * (N + 1)
    for j in range(i + 1):
        R[P[j]] += 1
    for j in range(1, N + 1):
        R[j] += R[j - 1]

    sm = 0
    for j in range(i + 1, N):
        sm += R[N] - R[P[j]]
        D[i + 1][j + 1] = sm

m = Manager(D)
dp = [[N * N] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
for j in range(N):
    m.add(j, dp[j][j])
    for i in range(j + 1, N + 1):
        dp[i][j + 1] = m.get_min(i)
        if i != N:
            m.add(i, dp[i][j])
    m.clear_log()

for i in range(1, N + 1):
    print(dp[N][i])
0