1 solutions

  • 0
    @ 2024-10-4 18:56:51

    image

    #include<bits/stdc++.h>
    #define For(i,l,r) for(int i=(l);i<=(r);++i)
    typedef long long ll;
    using namespace std;
    int gcd(int a,int b){return (b==0)?(a):(gcd(b,(a%b)));}
    int n;
    void solve()
    {
        scanf("%d",&n);
        int w=1,cnt=1,tmp=0;
        bool flag=true;
        For(i,1,n)
        {
            int opt;
            scanf("%d",&opt);
            if(opt==1)
            {
                ++w;
                ++cnt;
            }
            else if(opt==-1)
            {
                if(cnt>1)
                    --cnt;
                else if(cnt==1)
                {
                    if(tmp==0)
                        flag=false;
                    else
                    {
                        --tmp;
                        ++w;
                        ++cnt;
                    }
                }
            }
            else if(opt==0)
            {
                if(cnt==1)
                {
                    ++cnt;
                    ++w;
                }
                else
                {
                    --cnt;
                    ++tmp;
                }
            }
        }
        if(flag==false)
        {
            puts("-1");
            return;
        }
        else if(flag==true)
        {
            int GCD=gcd(w,cnt);
            w/=GCD;
            cnt/=GCD;
            printf("%d %d\n",w,cnt);
            return;
        }
        return;
    }
    int main()
    {
        //freopen("bag2.in","r",stdin);
        //freopen("bag2.ans","w",stdout);
        int t;
        scanf("%d",&t);
        while(t--)
            solve();
        return 0;
    }
    
    
    • 1

    Information

    ID
    540
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    7
    Accepted
    3
    Uploaded By