546C Codeforces - Soldier and Cards





Solution in c++

#include <iostream>

#include <deque>

using namespace std;

int n,sz1,sz2,t,stp;

deque<int> deq1,deq2;

int main()

{
    cin>>n;
    cin>>sz1;
    for(int i=0;i<sz1;i++)
    {
        cin>>t;
        deq1.push_back(t);
    }
    cin>>sz2;
    for(int i=0;i<sz2;i++)
    {
        cin>>t;
        deq2.push_back(t);
    }

    while(sz1 && sz2)
    {
        stp++;
        int t1=deq1.front(),t2=deq2.front(),t3=deq1.back(),t4=deq2.back();
        if(stp>1000){
            cout<<"-1\n";
            return 0;
        }
        deq1.pop_front();deq2.pop_front();
        sz1--;sz2--;
        if(t1>t2){
            deq1.push_back(t2);deq1.push_back(t1);
            sz1+=2;
        }
        else{
            deq2.push_back(t1);deq2.push_back(t2);
            sz2+=2;
        }
    }

    cout<<stp<<" "<<((sz1==0)?2:1);

    return 0;
}

Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

0 التعليقات:

إرسال تعليق