結果

問題 No.2029 Swap Min Max Min
ユーザー tamatotamato
提出日時 2022-08-05 23:06:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,017 bytes
コンパイル時間 793 ms
コンパイル使用メモリ 86,500 KB
実行使用メモリ 111,172 KB
最終ジャッジ日時 2023-10-14 00:51:57
合計ジャッジ時間 8,789 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,044 KB
testcase_01 AC 69 ms
70,928 KB
testcase_02 AC 69 ms
70,932 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 119 ms
89,940 KB
testcase_06 AC 147 ms
107,316 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 103 ms
82,600 KB
testcase_11 WA -
testcase_12 AC 126 ms
94,536 KB
testcase_13 WA -
testcase_14 AC 165 ms
105,728 KB
testcase_15 WA -
testcase_16 AC 173 ms
105,672 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 178 ms
105,464 KB
testcase_20 AC 178 ms
105,416 KB
testcase_21 AC 183 ms
105,612 KB
testcase_22 AC 178 ms
105,764 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 178 ms
105,680 KB
testcase_26 AC 179 ms
105,588 KB
testcase_27 WA -
testcase_28 AC 77 ms
75,244 KB
testcase_29 AC 72 ms
71,012 KB
testcase_30 AC 76 ms
75,176 KB
testcase_31 AC 79 ms
75,632 KB
testcase_32 WA -
testcase_33 AC 71 ms
71,252 KB
testcase_34 WA -
testcase_35 AC 72 ms
71,144 KB
testcase_36 AC 72 ms
70,968 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 176 ms
105,708 KB
testcase_40 AC 167 ms
110,012 KB
testcase_41 AC 196 ms
106,976 KB
testcase_42 AC 196 ms
107,204 KB
testcase_43 AC 173 ms
105,564 KB
testcase_44 AC 191 ms
111,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353


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 = int(input())
    A = list(map(int, input().split()))

    X = N // 2

    C = []
    cnt0 = cnt1 = 0
    for a in A:
        if a <= X:
            C.append(2 + cnt1 * 2)
            cnt1 += 1
        else:
            C.append(1 + cnt0 * 2)
            cnt0 += 1

    bit = Bit(N)
    idx = [0] * (N + 1)
    for i, c in enumerate(C):
        idx[c] = i + 1

    Y = 0
    for x in range(N, 0, -1):
        i = idx[x]
        Y += bit.sum(i)
        bit.add(i, 1)

    if N & 1:
        print(X, Y)
        exit()

    C = []
    D = []
    cnt0 = cnt1 = 0
    for a in A:
        if a > X:
            C.append(2 + cnt1 * 2)
            cnt1 += 1
            D.append(0)
        else:
            C.append(1 + cnt0 * 2)
            cnt0 += 1
            D.append(1)

    Y2 = 0
    cnt = 0
    flg = 0
    for i, d in enumerate(D):
        if d:
            if flg == 0:
                if cnt * 2 + 1 >= i:
                    Y2 += i - (cnt * 2 + 1)
                else:
                    flg = 1
                    Y2 += (cnt * 2) - i
            else:
                Y2 += abs(cnt * 2 - i)
            cnt += 1

    print(X, min(Y, Y2))


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