結果

問題 No.1051 PQ Permutation
ユーザー 👑 tamatotamato
提出日時 2020-05-08 22:28:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 240 ms / 2,000 ms
コード長 2,570 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 87,496 KB
実行使用メモリ 109,904 KB
最終ジャッジ日時 2023-09-19 07:47:02
合計ジャッジ時間 8,983 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,808 KB
testcase_01 AC 73 ms
71,792 KB
testcase_02 AC 73 ms
71,600 KB
testcase_03 AC 74 ms
71,748 KB
testcase_04 AC 76 ms
71,768 KB
testcase_05 AC 72 ms
71,552 KB
testcase_06 AC 73 ms
71,860 KB
testcase_07 AC 75 ms
71,688 KB
testcase_08 AC 73 ms
71,452 KB
testcase_09 AC 73 ms
71,424 KB
testcase_10 AC 74 ms
71,436 KB
testcase_11 AC 75 ms
71,676 KB
testcase_12 AC 74 ms
71,596 KB
testcase_13 AC 74 ms
71,696 KB
testcase_14 AC 75 ms
71,864 KB
testcase_15 AC 240 ms
109,400 KB
testcase_16 AC 228 ms
109,496 KB
testcase_17 AC 157 ms
109,528 KB
testcase_18 AC 148 ms
109,700 KB
testcase_19 AC 149 ms
109,476 KB
testcase_20 AC 210 ms
109,516 KB
testcase_21 AC 207 ms
109,500 KB
testcase_22 AC 148 ms
109,904 KB
testcase_23 AC 208 ms
109,516 KB
testcase_24 AC 205 ms
109,820 KB
testcase_25 AC 101 ms
78,648 KB
testcase_26 AC 87 ms
77,184 KB
testcase_27 AC 94 ms
77,004 KB
testcase_28 AC 93 ms
80,404 KB
testcase_29 AC 86 ms
77,008 KB
testcase_30 AC 104 ms
78,216 KB
testcase_31 AC 92 ms
77,516 KB
testcase_32 AC 98 ms
78,428 KB
testcase_33 AC 84 ms
77,076 KB
testcase_34 AC 92 ms
77,276 KB
testcase_35 AC 150 ms
91,064 KB
testcase_36 AC 141 ms
91,968 KB
testcase_37 AC 149 ms
107,724 KB
testcase_38 AC 223 ms
107,940 KB
testcase_39 AC 201 ms
109,492 KB
testcase_40 AC 168 ms
107,776 KB
testcase_41 AC 190 ms
107,660 KB
testcase_42 AC 77 ms
71,376 KB
testcase_43 AC 79 ms
71,768 KB
testcase_44 AC 76 ms
71,700 KB
testcase_45 AC 75 ms
71,468 KB
testcase_46 AC 73 ms
71,696 KB
testcase_47 AC 74 ms
71,696 KB
testcase_48 AC 231 ms
109,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    class Bit:
        def __init__(self, n):
            self.size = n
            self.tree = [0] * (n + 1)

        def sum(self, i):
            s = 0
            while i > 0:
                s += self.tree[i]
                i -= i & -i
            return s

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

        def lower_bound(self, w):
            if w <= 0:
                return 0
            x = 0
            k = 1 << (self.size.bit_length() - 1)
            while k:
                if x + k <= self.size and self.tree[x + k] < w:
                    w -= self.tree[x + k]
                    x += k
                k >>= 1
            return x + 1

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

    # increment
    j = N-1
    prev = 0
    bit = Bit(N+1)
    tmp = []
    while True:
        bit.add(A[j], 1)
        tmp.append(A[j])
        if A[j] > prev:
            prev = A[j]
            j -= 1
        else:
            break
        if j == -1:
            print(-1)
            exit()
    k = bit.lower_bound(bit.sum(A[j]) + 1)
    A[j] = k
    tmp.remove(k)
    tmp.sort()
    for jj in range(j+1, N):
        A[jj] = tmp[jj-j-1]

    for i, a in enumerate(A):
        if a == P:
            ip = i
        elif a == Q:
            iq = i
    if ip < iq:
        print(*A)
        exit()

    # iq change ok?
    bit = Bit(N)
    tmp = []
    for i in range(N-1, iq-1, -1):
        bit.add(A[i], 1)
        tmp.append(A[i])
    R = bit.lower_bound(bit.sum(Q) + 1)
    if R < N+1:
        A[iq] = R
        tmp.remove(R)
        if P > Q:
            tmp.remove(Q)
            tmp.append(P+0.5)
        tmp.sort()
        for i in range(iq+1, N):
            a = tmp[i-iq-1]
            if a == P+0.5:
                a = Q
            A[i] = a
        print(*A)
        exit()

    # iq change ng
    for i in range(iq-1, -1, -1):
        a = A[i]
        bit.add(a, 1)
        tmp.append(a)
        R = bit.lower_bound(bit.sum(a) + 1)
        if R >= Q:
            continue

        A[i] = R
        tmp.remove(R)
        if P > Q:
            tmp.remove(Q)
            tmp.append(P + 0.5)
        tmp.sort()
        for ii in range(i + 1, N):
            a = tmp[ii - i - 1]
            if a == P + 0.5:
                a = Q
            A[ii] = a
        print(*A)
        exit()

    print(-1)


if __name__ == '__main__':
    main()
0