結果

問題 No.1437 01 Sort
ユーザー tyawanmusityawanmusi
提出日時 2021-02-20 16:33:14
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,163 ms / 2,000 ms
コード長 2,211 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 91,160 KB
最終ジャッジ日時 2023-10-19 16:16:10
合計ジャッジ時間 14,543 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,560 KB
testcase_01 AC 36 ms
53,560 KB
testcase_02 AC 36 ms
53,560 KB
testcase_03 AC 36 ms
53,560 KB
testcase_04 AC 41 ms
53,560 KB
testcase_05 AC 36 ms
53,560 KB
testcase_06 AC 36 ms
53,560 KB
testcase_07 AC 36 ms
53,560 KB
testcase_08 AC 40 ms
53,560 KB
testcase_09 AC 40 ms
53,560 KB
testcase_10 AC 37 ms
53,560 KB
testcase_11 AC 37 ms
53,560 KB
testcase_12 AC 37 ms
53,560 KB
testcase_13 AC 1,031 ms
90,512 KB
testcase_14 AC 1,012 ms
90,476 KB
testcase_15 AC 481 ms
76,580 KB
testcase_16 AC 940 ms
88,392 KB
testcase_17 AC 599 ms
83,652 KB
testcase_18 AC 911 ms
88,680 KB
testcase_19 AC 731 ms
86,712 KB
testcase_20 AC 588 ms
83,140 KB
testcase_21 AC 1,057 ms
90,896 KB
testcase_22 AC 1,113 ms
90,896 KB
testcase_23 AC 1,067 ms
90,896 KB
testcase_24 AC 1,163 ms
91,160 KB
testcase_25 AC 1,060 ms
90,632 KB
testcase_26 AC 1,069 ms
90,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# writer 解
# O(N log^2 N)

# にぶたんしてくれるやつ
from bisect import bisect_left, bisect_right

n = int(input())
s = input()
ans = 10**10

zero = s.count("0")
one = n-zero
if zero == 0 or zero == n:
  exit(print(0))

# 注意事項
out = [0]*n
if s[0] == "1":
  out[0] = 1
  for i in range(n-1, -1, -1):
    if s[i] == "1":
      out[i] = 1
    else:
      break

# "1" である index をとりあえずいっぱい列挙
bit = []
for i in range(n):
  if s[i] == "1":
    bit += [i, i+n, i+n+n, i+n+n+n]
bit.sort()

def f(l, ll):
  # bit[ll, ll+one) を [l, l+one) に揃えるとき、どれだけグルグル回るか

  #注意事項 
  sp_l = (l+one)%n
  sp_r = n
  if sp_l == 0:
    sp_l = n
  
  ansl = 0
  if bit[ll] <= l+n:
    # 注意事項
    # ((x-1)%n+1) で 0...n-1 ではなく 1...n にする
    if sp_l <= ((bit[ll]-1)%n+1) <= sp_r and out[bit[ll]%n] == 0:
      ansl = l+n-bit[ll]-1
    else:
      ansl = l+n-bit[ll]
  
  ansr = 0
  if l+n+one-1 < bit[ll+one-1]:
    # "右側" である要素を数えるにぶたん
    ng = -1
    ok = one
    while ng+1 != ok:
      mid = (ng+ok)//2
      if l+n+mid < bit[ll+mid]:
        ok = mid
      else:
        ng = mid
    
    # 注意事項
    flag1 = True
    flag2 = False
    x = l+n+ok
    if x%n != 0:
      x += n-(x%n)
    if x <= l+n+one-1:
      if l+n+ok <= bit[ll+ok] <= x:
        flag2 = True
      x += n
    if l+n+ok <= bit[ll+ok] <= x:
      flag1 = False
    ansr = one-ok
    ansr += flag1
    ansr -= flag2
  return max(ansl, ansr)

# 目標の区間を全探索
for l in range(n):
  # グルグル回る以前の操作回数
  anss = (-l-one)%n
  
  # 注意事項
  sp_l = (l+one)%n
  sp_r = n
  if sp_l == 0:
    sp_l = n
  
  # 何回グルグル回れば揃えられるかにぶたん
  ng = -1
  ok = n
  while ng+1 != ok:
    mid = (ng+ok)//2
    
    # 回る回数を決め打ちし、bit[ll, ll+one) の ll をできるだけ小さくする
    
    # 注意事項
    x = l+n-mid-1
    if not(sp_l <= ((x-1)%n+1) <= sp_r and out[x%n] == 0):
      x += 1
    ll = bisect_left(bit,x)
    
    if f(l, ll) <= mid:
      ok = mid
    else:
      ng = mid
  
  ans = min(ans, anss+ok*n)
print(ans)
0