Submission #62782846


Source Code Expand

Copy
n = int(input())
s = input()
a = [i for i in range(n) if s[i] == "1"]
# print(a)
def f(x):
ret = 0
for i in range(len(a)):
ret += abs(a[i] - x)
x += 1
return ret
def ternary_search(f, l, r):
while r - l > 2:
m1 = l + (r - l) // 3
m2 = r - (r - l) // 3
# print(l, r, m1, m2, f(m1), f(m2))
if f(m1) > f(m2):
l = m1
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
n = int(input())
s = input()
a = [i for i in range(n) if s[i] == "1"]
# print(a)


def f(x):
    ret = 0
    for i in range(len(a)):
        ret += abs(a[i] - x)
        x += 1
    return ret


def ternary_search(f, l, r):
    while r - l > 2:
        m1 = l + (r - l) // 3
        m2 = r - (r - l) // 3
        # print(l, r, m1, m2, f(m1), f(m2))
        if f(m1) > f(m2):
            l = m1
        else:
            r = m2

    ret = f(l)
    for x in range(l, r + 1):
        ret = min(ret, f(x))
    return ret


print(ternary_search(f, 0, n - 1))

Submission Info

Submission Time
Task D - Swap to Gather
User okapin
Language Python (PyPy 3.10-v7.3.12)
Score 425
Code Size 584 Byte
Status AC
Exec Time 149 ms
Memory 118204 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status
AC × 3
AC × 28
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.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 55 ms 76232 KB
00_sample_01.txt AC 55 ms 76452 KB
00_sample_02.txt AC 56 ms 76772 KB
01_random_00.txt AC 66 ms 82480 KB
01_random_01.txt AC 102 ms 97160 KB
01_random_02.txt AC 89 ms 92160 KB
01_random_03.txt AC 58 ms 81972 KB
01_random_04.txt AC 69 ms 84660 KB
01_random_05.txt AC 79 ms 88316 KB
01_random_06.txt AC 90 ms 92488 KB
01_random_07.txt AC 99 ms 95764 KB
01_random_08.txt AC 109 ms 99592 KB
01_random_09.txt AC 122 ms 104512 KB
01_random_10.txt AC 129 ms 107412 KB
01_random_11.txt AC 134 ms 110684 KB
01_random_12.txt AC 145 ms 114596 KB
01_random_13.txt AC 145 ms 118204 KB
02_random2_00.txt AC 93 ms 94056 KB
02_random2_01.txt AC 149 ms 114760 KB
02_random2_02.txt AC 73 ms 85728 KB
02_random2_03.txt AC 63 ms 82272 KB
02_random2_04.txt AC 75 ms 87472 KB
02_random2_05.txt AC 97 ms 95944 KB
03_handmade_00.txt AC 55 ms 76508 KB
03_handmade_01.txt AC 55 ms 76644 KB
03_handmade_02.txt AC 56 ms 76520 KB
03_handmade_03.txt AC 55 ms 76480 KB
03_handmade_04.txt AC 112 ms 99864 KB


2025-04-04 (Fri)
03:17:43 +00:00