1553C - Penalty - CodeForces Solution


bitmasks brute force dp greedy *1200

Please click on ads to support us..

Python Code:

t = int(input())

def can_stop(first_score, second_score, num_kicks):
    if ((second_score < first_score) and (num_kicks % 2 == 1)):
        return abs(first_score - (second_score + 1)) > ((10 - num_kicks)//2)
    return abs(first_score - (second_score)) > ((10 - num_kicks)//2)

for i in range(t):
    s = input()
    first_score = 0
    second_score = 0

    first_case = 1
        while (first_case < 10):
        if (first_case % 2 == 1 and s[first_case-1] != '0'):
            first_score += 1
        elif (first_case % 2 == 0 and s[first_case-1] == '1'):
            second_score += 1

        if (can_stop(first_score, second_score, first_case)):
            break

        first_case += 1
    
    first_score = 0
    second_score = 0
    second_case = 1
        while (second_case < 10):
        if (second_case % 2 == 1 and s[second_case-1] == '1'):
            first_score += 1
        elif (second_case % 2 == 0 and s[second_case-1] != '0'):
            second_score += 1
     
        if (can_stop(first_score, second_score, second_case)):
            break

        second_case += 1
    
    print(min(first_case, second_case))


    

C++ Code:

#include <bits/stdc++.h>
#ifdef DEBUG
#include "./../Algorithms/competitive_programming/debug.h"
#else
#define debug(...) 42
#endif
using namespace  std;
#define ll long long

int check(string& s) {
    vector<int> a, b;
    for (int i = 0; i < 10; i += 2) {
        a.push_back(s[i] == '1');
        int n = a.size() - 1;
        if (i)a[n] += a[n - 1];
    }
    // debug(a);
    for (int i = 1; i < 10; i += 2) {
        b.push_back(s[i] == '1');
        int n = b.size() - 1;
        if (i > 1)b[n] += b[n - 1];
    }
    int ans = 10;
    for (int i = 0; i < 5; i++) {
        int a_did = a[i];
        int a_have = 5 - i - 1;
        int b_did = (i ? b[i - 1] : 0);
        int b_have = 5 - i;
        if (a_did > b_did + b_have || a_did + a_have < b_did) {
            ans = min(ans, 2 * i + 1);
        }

        b_did = b[i];
        b_have = 5 - i - 1;
        if (a_did > b_did + b_have || a_did + a_have < b_did) {
            ans = min(ans, 2 * i + 2);
        }
        // debug(i, a_did, a_have, b_did, b_have);
    }
    // debug(ans);
    return ans;
}
inline void solve() {
    string s; cin >> s;
    // a best      worst
    string s1 = s, s2 = s;
    for (int i = 0; i < 10; i++) {
        if (s[i] == '?') {
            if (i & 1) {
                s1[i] = '0';
                s2[i] = '1';
            }
            else {
                s1[i] = '1';
                s2[i] = '0';
            }
        }
    }
    // debug(s1, s2);
    cout << min(check(s1), check(s2)) << endl;
    return;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)solve();
}


Comments

Submit
0 Comments
More Questions

1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase