Submission #69854677


Source Code Expand

from itertools import accumulate

for _ in range(int(input())):
  n = int(input())
  S = list(map(int, input()))
  T0_ = [1 if S[i]==0 else 0 for i in range(n)]
  T1_ = [1 if S[i]==1 else 0 for i in range(n)]
  T0 = list(accumulate(T0_))
  T1 = list(accumulate(T1_))
  
  l = 0
  ans = n**2
  while l<n:
    r = l+1
    while r<n and S[l]==S[r]:
      r += 1

    t0 = (T0[l-1] if l-1>=0 else 0)+T0[n-1]-T0[r-1]
    t1 = (T1[l-1] if l-1>=0 else 0)+T1[n-1]-T1[r-1]
    if S[l]==0:
      ans = min(ans, t0*2+t1)
    else:
      ans = min(ans, t1*2+t0)
    l = r
  print(ans)

Submission Info

Submission Time
Task D - Pop and Insert
User uparupaaa
Language Python (PyPy 3.10-v7.3.12)
Score 400
Code Size 596 Byte
Status AC
Exec Time 244 ms
Memory 167392 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 1
AC × 14
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt, 03_handmade_04.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 59 ms 76608 KiB
01_small_00.txt AC 244 ms 86860 KiB
02_random_00.txt AC 131 ms 84604 KiB
02_random_01.txt AC 115 ms 88020 KiB
02_random_02.txt AC 116 ms 136388 KiB
02_random_03.txt AC 126 ms 167392 KiB
02_random_04.txt AC 125 ms 167120 KiB
02_random_05.txt AC 131 ms 167208 KiB
02_random_06.txt AC 134 ms 167136 KiB
03_handmade_00.txt AC 124 ms 166368 KiB
03_handmade_01.txt AC 123 ms 166264 KiB
03_handmade_02.txt AC 128 ms 167336 KiB
03_handmade_03.txt AC 125 ms 167188 KiB
03_handmade_04.txt AC 125 ms 167160 KiB