Codeforces:Penalty

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 si is 1, then the i-th kick will definitely score a goal;
if si is 0, then the i-th kick definitely won’t score a goal;
if si 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≤t≤1000) — 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.

Example
input

4
1?0???1001
1111111111
???
0100000000

output

7
10
6
9

题解

我们可以发现,决定踢球的最少次数取决于?,所以我们可以分两种情况①一队所有?全部得分,二队?全部不得分 ②一队全部?不得分,二队?全部得分。那么答案就在这两种情况中产生,然后分别模拟即可

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll t;
string s;
string s1,s2;
int arr[10];
int brr[10];
//分开上面两种情况进行讨论
void change(int k,int k1)
{
     for(int i=0; i<5; i++)
        {
            if(s1[i]=='?')arr[i]=k;
            else arr[i] = s1[i]-'0';
            if(s2[i]=='?')brr[i]=k1;
            else brr[i] = s2[i]-'0';
        }
}
//用来判断何时停止点球,sum是一队的得分,num是二队的得分
int add(int sum,int num)
{
    int ans=0;
    for(int i=0; i<5; i++)
        {
            if(arr[i]==1)
                sum++;
            ans++;
            if(sum-num>4-i+1||num-sum>4-i)
                return ans;
            if(brr[i]==1)
                num++;
            ans++;
            if(abs(sum-num>4-i)>0)
                return ans;
        }
        return ans;
}
int main(void)
{
    cin>>t;
    while(t--)
    {
        int sum=0,num=0;
        cin>>s;
        int a=0,b=0;
        int ans=0;
        int ans2=0;
     
        for(int i=0; i<10; i++)
        {   //将二队得分分开两个数组方便比较
            if(i%2==0)s1[a++]=s[i];
            else s2[b++]=s[i];
        }
        change(1,0);
        ans = add(sum,num);
        sum=0,num=0;
        change(0,1);
        ans2 = add(sum,num);
        //两种情况取最小值
        cout<<min(ans2,ans)<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值