QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1146#723735#9565. Birthday Giftucup-team1196ucup-team1196Success!2024-11-08 00:03:152024-11-08 00:03:15

Details

Extra Test:

Runtime Error

input:

1
222

output:


result:


IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#723735#9565. Birthday Giftucup-team1196RE 13ms9204kbC++231.1kb2024-11-07 23:58:352024-11-08 00:03:51

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve(int t) {
    string s;
    cin>>s;
    int n=s.size();
    if(n==1){
        cout<<1<<"\n";
        return;
    }

    if (t != 5) assert(n % 2 == 0);
    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;

    for (int i = 1; i <= t; i++) {
        solve(t);
    }
}