結果

問題 No.1437 01 Sort
ユーザー zkouzkou
提出日時 2021-03-23 22:27:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 604 ms / 2,000 ms
コード長 3,266 bytes
コンパイル時間 128 ms
コンパイル使用メモリ 82,704 KB
実行使用メモリ 108,912 KB
最終ジャッジ日時 2024-05-04 14:25:04
合計ジャッジ時間 8,035 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
55,296 KB
testcase_01 AC 44 ms
55,168 KB
testcase_02 AC 38 ms
55,040 KB
testcase_03 AC 40 ms
55,424 KB
testcase_04 AC 42 ms
55,168 KB
testcase_05 AC 39 ms
55,040 KB
testcase_06 AC 42 ms
55,680 KB
testcase_07 AC 42 ms
55,552 KB
testcase_08 AC 42 ms
55,424 KB
testcase_09 AC 42 ms
55,040 KB
testcase_10 AC 42 ms
55,936 KB
testcase_11 AC 49 ms
55,296 KB
testcase_12 AC 42 ms
56,192 KB
testcase_13 AC 331 ms
106,284 KB
testcase_14 AC 352 ms
106,672 KB
testcase_15 AC 80 ms
94,948 KB
testcase_16 AC 604 ms
96,972 KB
testcase_17 AC 438 ms
90,624 KB
testcase_18 AC 546 ms
97,920 KB
testcase_19 AC 494 ms
92,800 KB
testcase_20 AC 457 ms
92,032 KB
testcase_21 AC 563 ms
108,912 KB
testcase_22 AC 546 ms
106,612 KB
testcase_23 AC 555 ms
108,048 KB
testcase_24 AC 547 ms
108,896 KB
testcase_25 AC 565 ms
107,248 KB
testcase_26 AC 450 ms
104,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import lru_cache

def solve(N, S):
    if list(S) == sorted(S):
        return 0

    S2 = S * 2
    
    # get #0 in S[l:r] by zero_cnt[r] - zero_cnt[l]
    zero_cnt = [0]
    one_cnt = [0]
    for c in S2:
        zero_cnt.append(zero_cnt[-1] + int(c == '0'))
        one_cnt.append(one_cnt[-1] + int(c == '1'))

    @lru_cache(10)
    def calc(l, t, r):
        left0 = zero_cnt[t + 1] - zero_cnt[l]
        right1 = one_cnt[r + 1] - one_cnt[t + 1]
        end = t + right1
        rem = (N - 1 - end) % N
        assert l <= t <= end <= r
        if end < N:
            if right1:
                rskip = int(bool(one_cnt[N + 1] - one_cnt[t + 1]))
                right1 = right1 + 1 - rskip
            loop = max(left0, right1)
        elif t < N:
            rskip = int(bool(one_cnt[N + 1] - one_cnt[t + 1]))
            loop = max(left0 - 1, right1 - rskip)
        else:
            lskip = int(bool(zero_cnt[N + 1] - zero_cnt[l + 1]))
            loop = max(left0 - lskip, right1)
        loop = max(0, loop)
        return loop * N + rem

    ones = []
    for i, c in enumerate(S2):
        if c == '1':
            ones.append(i)

    answer = 10 ** 18
    
    # debug = []
    t = -1
    for i in range(one_cnt[N]):
        l = ones[i]
        r = ones[i + one_cnt[N] - 1]
        # debug.append([-1] * l + [calc(l, t, r) for t in range(l, r + 1)])
        t = max(l, t)
        while True:
            c = calc(l, t, r)
            # print(l, t, r, c)
            nt = t + 1
            while nt <= r and c == calc(l, nt, r):
                nt += 1
            if nt == r + 1 or c < calc(l, nt, r):
                break
            t = nt
        answer = min(answer, c)        
    
    # print(*debug, sep='\n')

    return answer


def slow(N, S):
    parent = dict()
    sorted_S = sorted(S)
    q = [(0, list(S))]
    while True:
        nq = []
        for t, Sp in q:
            # print(Sp, sorted_S)
            if Sp == sorted_S:
                tr = []
                Sp_str = ''.join(Sp)
                while Sp_str != S:
                    tr.append(Sp_str)
                    Sp_str = parent[Sp_str]
                tr.append(S)
                print(tr[::-1])
                for i in range(len(tr)):
                    k = i % N
                    print(tr[-i-1][k:] + tr[-i-1][:k])
                return t
            newSp = [Sp[-1]] + Sp[:-1]
            nq.append((t + 1, newSp))
            if ''.join(newSp) not in parent:
                parent[''.join(newSp)] = ''.join(Sp)
            newSp = [Sp[0]] + [Sp[-1]] + Sp[1:-1]
            nq.append((t + 1, newSp))
            if ''.join(newSp) not in parent:
                parent[''.join(newSp)] = ''.join(Sp)
        q = nq
    

def main():
    N = int(input())
    S = input()
    print(solve(N, S))
    # print(slow(N, S))

def test(t):
    import random
    for r in range(t):
        S = bin(r)[2:]
        N = len(S)
        # print(N, S)
        ac = slow(N, S)
        wa = solve(N, S)
        # print(ac, wa)
        assert ac == wa
        S = S.replace('1', '2').replace('0', '1').replace('2', '0')
        # print(N, S)
        ac = slow(N, S)
        wa = solve(N, S)
        # print(ac, wa)
        assert ac == wa

main()
# test(1000)
0