Codeforces Round #304 (Div. 2)

来源:互联网 发布:ubuntu 查看系统日志 编辑:程序博客网 时间:2024/05/16 10:04

C. Soldier and Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to nall values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game.

The rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins.

You have to calculate how many fights will happen and who will win the game, or state that game won't end.

Input

First line contains a single integer n (2 ≤ n ≤ 10), the number of cards.

Second line contains integer k1 (1 ≤ k1 ≤ n - 1), the number of the first soldier's cards. Then follow k1 integers that are the values on the first soldier's cards, from top to bottom of his stack.

Third line contains integer k2 (k1 + k2 = n), the number of the second soldier's cards. Then follow k2 integers that are the values on the second soldier's cards, from top to bottom of his stack.

All card values are different.

Output

If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won.

If the game won't end and will continue forever output  - 1.

Examples
input
42 1 32 4 2
output
6 2
input
31 22 1 3
output
-1
Note

First sample:

Second sample:

码力题。。

#include<cstdio>#include<cstdlib>#include<iostream>#include<stack>#include<queue>#include<algorithm>#include<string>#include<cstring>#include<cmath>#include<vector>#include<map>#include<set>#define eps 1e-8#define zero(x) (((x>0?(x):-(x))-eps)#define mem(a,b) memset(a,b,sizeof(a))#define memmax(a) memset(a,0x3f,sizeof(a))#define pfn printf("\n")#define ll __int64#define ull unsigned long long#define sf(a) scanf("%d",&a)#define sf64(a) scanf("%I64d",&a)#define sf264(a,b) scanf("%I64d%I64d",&a,&b)#define sf364(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)#define sf464(a,b,c,d) scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)#define sf564(a,b,c,d,ee) scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&ee)#define sf2(a,b) scanf("%d%d",&a,&b)#define sf3(a,b,c) scanf("%d%d%d",&a,&b,&c)#define sf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)#define sf5(a,b,c,d,ee) scanf("%d%d%d%d%d",&a,&b,&c,&d,&ee)#define sff(a) scanf("%f",&a)#define sfs(a) scanf("%s",a)#define sfs2(a,b) scanf("%s%s",a,b)#define sfs3(a,b,c) scanf("%s%s%s",a,b,c)#define sfd(a) scanf("%lf",&a)#define sfd2(a,b) scanf("%lf%lf",&a,&b)#define sfd3(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)#define sfd4(a,b,c,d) scanf("%lf%lf%lf%lf",&a,&b,&c,&d)#define sfc(a) scanf("%c",&a)#define ull unsigned long long#define pp pair<int,int>#define debug printf("***\n")#define pi 3.1415927#define mod 1000000007#define rep(i,a,b) for(int i=a;i<b;i++)const double PI = acos(-1.0);const double e = exp(1.0);const int INF = 0x7fffffff;;template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }bool cmpbig(int a, int b){ return a>b; }bool cmpsmall(int a, int b){ return a<b; }using namespace std;vector<int>vis[100010];vector<int>vis2[100010];int main(){   // freopen("data.in","r",stdin);    //freopen("data.out" ,"w",stdout);    int n;    while(cin>>n)    {        //int k1,k2;        int n,k11,k22;        queue<int>q1,q2;        sf(k11);        while(!q1.empty())            q1.pop();        while(!q2.empty())            q2.pop();        rep(i,0,k11)        {            int pos;            sf(pos);            q1.push(pos);        }        rep(i,0,100010)        {            vis[i].clear();            vis2[i].clear();        }        sf(k22);        rep(i,0,k22)        {            int pos;            sf(pos);            q2.push(pos);        }        int cnt=0;        while(1)        {            if(q1.empty()||q2.empty())                break;            int fff=0,k=0,k2=0,fff2=0;            int pos[20],pos2[20];            mem(pos,0),mem(pos2,0);            while(!q1.empty())            {                pos[k++]=q1.front();                q1.pop();            }            rep(i,0,k)                q1.push(pos[i]);            while(!q2.empty())            {                pos2[k2++]=q2.front();                q2.pop();            }            rep(i,0,k2)                q2.push(pos2[i]);            int flagg;            rep(i,0,cnt)            {                fff=0;                fff2=0;                flagg=0;                if(vis[i].size()!=k)                    continue;                if(vis2[i].size()!=k2)                    continue;                rep(j,0,k)                {                    if(vis[i][j]==pos[j])                        fff++;                    else                        break;                }                rep(j,0,k2)                {                    if(vis2[i][j]==pos2[j])                        fff2++;                    else                        break;                }                if(fff2==k2)                    flagg++;                if(fff==k)                    flagg++;                if(flagg==2)                {                    cout<<-1<<endl;                    return 0;                }            }//            rep(i,0,k)//                cout<<pos[i]<<" ";//                pfn;//            rep(i,0,k2)//                cout<<pos2[i]<<" ";//                pfn;            //cout<<fff<<" "<<k<<" "<<fff2<<" "<<k2<<endl;            rep(i,0,k)                vis[cnt].push_back(pos[i]);            rep(i,0,k2)                vis2[cnt].push_back(pos2[i]);            cnt++;            int p1=q1.front(),p2=q2.front();           // cout<<p1<<" "<<p2<<endl;            if(p1>p2)            {                q2.pop();                q1.pop();                q1.push(p2);                q1.push(p1);            }            else            {                q1.pop();                q2.pop();                //cout<<q2.top()<<endl;                q2.push(p1);                q2.push(p2);            }        }//        rep(i,0,cnt)//        {//            cout<<i+1<<endl;//            rep(j,0,vis[i].size())//                cout<<vis[i][j]<<" ";//                pfn;//            rep(j,0,vis2[i].size())//                cout<<vis2[i][j]<<" ";//            pfn;//        }        cout<<cnt<<" ";        if(q1.empty())            cout<<2<<endl;        else            cout<<1<<endl;    }    return 0;}



0 0
原创粉丝点击