QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#1148#713435#9565. Birthday Giftucup-team1196ucup-team1196Failed.2024-11-08 00:23:362024-11-08 00:23:36

详细

Extra Test:

Accepted
time: 1ms
memory: 3640kb

input:

50
110211020010110011100101101010000011022010111202011001001011102001101000011001211000221211110110000110021000110101120100110100011010011001110110011000101021122001001101101101101011010101110101100001111001011210211101201001200101010010011210001201111000000010000110101100120011001100001112121011100...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
17
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

ok 50 numbers

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#713435#9565. Birthday Giftucup-team1196#AC ✓12ms8780kbC++231019b2024-11-05 19:22:232024-11-09 01:53:38

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    string s;
    cin>>s;
    int n=s.size();
    if(n==1){
        cout<<1<<"\n";
        return;
    }
    stack<int>st;
    for(int i=0;i<n;++i){
        int x=s[i]-'0';
        if(!st.empty() && x!=2 && st.top()==x){
            st.pop();
        }
        else st.push(x);
    }
    vector<int>a;
    while(!st.empty()){
        a.push_back(st.top());
        st.pop();
    }
    int sz=a.size();
    int r[3]={0,0,0};
    int cur=0;
    for(int i=1;i<sz;i+=2){
        if(a[i]==2) continue;
        a[i]^=1;
    }
    for(int i=0;i<sz;++i) r[a[i]]++;
    int ma=max(r[0],r[1]),mi=min(r[0],r[1]);
    if(mi+r[2]>=ma){
        int ans=r[0]+r[1]+r[2];
        cout<<(ans&1)<<"\n";
    }
    else cout<<ma-mi-r[2]<<"\n";
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int t = 1;
    std::cin >> t;

    while (t --) {
        solve();
    }
}