CodeForces 546C Soldier and Cards (队列)

http://codeforces.com/problemset/problem/546/C

Soldier and Cards

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.

Sample test(s)
input
4
2 1 3
2 4 2
output
6 2
input
3
1 2
2 1 3
output
-1
Note

First sample:

CodeForces 546C Soldier and Cards (队列)_第1张图片

Second sample:

CodeForces 546C Soldier and Cards (队列)_第2张图片

题意:有两个玩家,每个玩家都拿出自己最顶部的牌放在桌子上比较大小,谁的牌大谁胜,然后胜者先将对方的桌子上的牌放到自己的最底部,然后再将自己桌子上的牌放到最底部,依次循环,知道最后一方的牌为空,则对方胜,如果两个玩家的牌都不可能为空,就输出-1;

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>

using namespace std;
typedef long long LL;

#define N 110000
#define INF 0x3f3f3f3f
#define PI acos (-1.0)
#define EPS 1e-8
#define met(a, b) memset (a, b, sizeof (a))

int main ()
{
    int n, m1, m2, Stack1, Stack2;

    while (scanf ("%d", &n) != EOF)
    {
        int cnt = 0, flag = 0;

        queue <int> que1;
        queue <int> que2;

        scanf ("%d", &m1);
        for (int i=1; i<=m1; i++)
        {
            scanf ("%d", &Stack1);
            que1.push (Stack1);
        }

        scanf ("%d", &m2);

        for (int i=1; i<=m2; i++)
        {
            scanf ("%d", &Stack2);
            que2.push (Stack2);
        }

        while (que1.size() && que2.size())
        {
            int x1 = que1.front(); que1.pop();
            int x2 = que2.front(); que2.pop();

            if (x1 > x2)
            {
                que1.push (x2);
                que1.push (x1);
            }
            else
            {
                que2.push (x1);
                que2.push (x2);
            }
            cnt++;

            if (!que1.size ()) flag = 2;
            else if (!que2.size()) flag = 1;

            if (cnt >= 10000)
            {
                cnt = -1;
                break;
            }
        }

        if (cnt != -1)
            printf ("%d %d\n", cnt, flag);
        else printf ("%d\n", cnt);
    }
    return 0;
}


你可能感兴趣的:(CodeForces 546C Soldier and Cards (队列))