1955E - Long Inversions - CodeForces Solution


brute force greedy implementation sortings

Please click on ads to support us..

Python Code:

def isPossible(list, k):
    inverts = [False for _ in list]

    inverted = False
    for i in range(len(list)-k):
        if inverts[i]:
            inverted = not inverted

        v = list[i]
        if inverted:
            v = not v

        if not v:
            inverts[i+k] = True
            inverted = not inverted

    zeroes = False
    ones = False
    for i in range(len(list)-k, len(list)):
        if inverts[i]:
            inverted = not inverted

        v = list[i]
        if inverted:
            v = not v

        if v:
            ones = True
        else:
            zeroes = True

    return not(zeroes and ones)
            
def solve():
    n = int(input())
    l = list( map(lambda x: True if x=='1' else False, input()))
    
    best = 0
    for i in range(1,n+1):
        if isPossible(l, i):
            best = i

    print(best)

testCases = int(input())

for i in range(testCases):
    solve()


Comments

Submit
0 Comments
More Questions

550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String