Penalty (Codeforces 1553C)
Tags: Greedy, bitmasks

Consider a simplified penalty phase at the end of a football match.A penalty phase consists of at most $$$10$$$ kicks, the first team takes the first kick, the second team takes the second kick, then the first team takes the third kick, and so on. The team that scores more goals wins; if both teams score the same number of goals, the game results in a tie (note that it goes against the usual football rules). The penalty phase is stopped if one team has scored more goals than the other team could reach with all of its remaining kicks. For example, if after the $$$7$$$-th kick the first team has scored $$$1$$$ goal, and the second team has scored $$$3$$$ goals, the penalty phase ends — the first team cannot reach $$$3$$$ goals.You know which player will be taking each kick, so you have your predictions for each of the $$$10$$$ kicks. These predictions are represented by a string $$$s$$$ consisting of $$$10$$$ characters. Each character can either be 1, 0, or ?. This string represents your predictions in the following way:  if $$$s_i$$$ is 1, then the $$$i$$$-th kick will definitely score a goal;  if $$$s_i$$$ is 0, then the $$$i$$$-th kick definitely won't score a goal;  if $$$s_i$$$ is ?, then the $$$i$$$-th kick could go either way. Based on your predictions, you have to calculate the minimum possible number of kicks there can be in the penalty phase (that means, the earliest moment when the penalty phase is stopped, considering all possible ways it could go). Note that the referee doesn't take into account any predictions when deciding to stop the penalty phase — you may know that some kick will/won't be scored, but the referee doesn't.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 1\,000$$$) — the number of test cases.Each test case is represented by one line containing the string $$$s$$$, consisting of exactly $$$10$$$ characters. Each character is either 1, 0, or ?.

Output: For each test case, print one integer — the minimum possible number of kicks in the penalty phase.

Note: Consider the example test:In the first test case, consider the situation when the $$$1$$$-st, $$$5$$$-th and $$$7$$$-th kicks score goals, and kicks $$$2$$$, $$$3$$$, $$$4$$$ and $$$6$$$ are unsuccessful. Then the current number of goals for the first team is $$$3$$$, for the second team is $$$0$$$, and the referee sees that the second team can score at most $$$2$$$ goals in the remaining kicks. So the penalty phase can be stopped after the $$$7$$$-th kick.In the second test case, the penalty phase won't be stopped until all $$$10$$$ kicks are finished.In the third test case, if the first team doesn't score any of its three first kicks and the second team scores all of its three first kicks, then after the $$$6$$$-th kick, the first team has scored $$$0$$$ goals and the second team has scored $$$3$$$ goals, and the referee sees that the first team can score at most $$$2$$$ goals in the remaining kicks. So, the penalty phase can be stopped after the $$$6$$$-th kick.In the fourth test case, even though you can predict the whole penalty phase, the referee understands that the phase should be ended only after the $$$9$$$-th kick.

Sample Inputs:
4
1?0???1001
1111111111
??????????
0100000000

Sample Outputs:
7
10
6
9

Solution (Python):
import sys

def solve(s):
    dp = [0, 0, 0, 0]
    n = len(s)
    (l1, l2) = (5, 5)
    for i in range(n):
        if i % 2 == 0:
            l1 -= 1
        if i % 2 != 0:
            l2 -= 1
        if s[i] == '1':
            if i % 2 == 0:
                dp[0] += 1
                dp[1] += 1
            else:
                dp[2] += 1
                dp[3] += 1
        if s[i] == '?':
            if i % 2 == 0:
                dp[0] += 1
            else:
                dp[2] += 1
        if dp[0] - dp[3] > l2 or dp[2] - dp[1] > l1:
            return i + 1
    return n
if __name__ == '__main__':
    try:
        t = int(input().strip())
        for _ in range(t):
            s = input().strip()
            result = solve(s)
            print(result)
    except Exception as e:
        print(f'Exception: {e}')
        sys.exit(1)

Solution (Python):
import sys
import copy
import heapq
import bisect
import functools
from math import *
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import groupby
import itertools
from itertools import accumulate
from decimal import Decimal, ROUND_HALF_UP
readline = lambda : sys.stdin.readline().rstrip('\r\n')

def solve():
    s = list(str(readline()))
    cnt1 = [0] * 2
    cntm = [0] * 2
    ans = 10
    for (i, x) in enumerate(s, 1):
        if x == '?':
            cntm[i & 1] += 1
        elif x == '1':
            cnt1[i & 1] += 1
        f1 = cnt1[0] + cntm[0] > (10 - i) // 2 + cnt1[1]
        f2 = cnt1[1] + cntm[1] > (11 - i) // 2 + cnt1[0]
        if f1 or f2:
            ans = min(ans, i)
            break
    print(ans)

def main():
    t = 1
    t = int(readline())
    for i in range(t):
        solve()
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    s = readStr()
    sa = sb = cnta = cntb = 0
    for i in range(10):
        cnta += 1 if s[i] == '?' and i % 2 == 0 else 0
        cntb += 1 if s[i] == '?' and i % 2 else 0
        sa += 1 if s[i] == '1' and i % 2 == 0 else 0
        sb += 1 if s[i] == '1' and i % 2 else 0
        na = nb = (10 - (i + 1)) // 2
        if (10 - (i + 1)) % 2:
            nb += 1
        (u, v) = (sa + cnta, sb)
        if u > v + nb:
            return i + 1
        (u, v) = (sa, sb + cntb)
        if u + na < v:
            return i + 1
    return 10
for _ in range(int(input())):
    print(solve())