Codeforces 546C Soldier and Cards【模拟】【期末考试全突击啊T T 】

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
4
2 1 3
2 4 2
output
6 2
input
3
1 2
2 1 3
output
-1
Note

First sample:

Second sample:

题目大意:

两个人玩纸牌游戏,每一轮游戏每个人都把自己的第一个牌拿出来比较大小,大的一方先将对面的这个牌放在自己牌的最后,再把自己的牌放在最后,直到一个人没有了纸牌就算那个人输,问一共会进行多少次游戏,以及赢的人的编号。


思路(明天考Java敲完这题就要滚去预习啦好不开心):


观察到N最大也就是10,那么我们用两个队列来模拟这个游戏过程即可。

处理细节注意不要拿空队列中的元素。


T T期末考试突击鸭梨大啊,我空寂不住我积极啊...........我想敲题啊.............T T


Ac代码:


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        queue<int >a;
        queue<int >b;
        int tmp;
        scanf("%d",&tmp);
        for(int i=0;i<tmp;i++)
        {
            int x;
            scanf("%d",&x);
            a.push(x);
        }
        scanf("%d",&tmp);
        for(int i=0;i<tmp;i++)
        {
            int x;
            scanf("%d",&x);
            b.push(x);
        }
        int cnt=0;
        int flag=0;
        while(cnt<=1400000)
        {
            int u=a.front();a.pop();
            int v=b.front();b.pop();
            cnt++;
            if(u>v)
            {
                a.push(v);
                a.push(u);
            }
            else
            {
                b.push(u);
                b.push(v);
            }
            if(a.size()==0&&b.size()==0)
            {
                break;
            }
            if(a.size()==0)
            {
                printf("%d %d\n",cnt,2);
                flag=1;
            }
            if(b.size()==0)
            {
                printf("%d %d\n",cnt,1);
                flag=1;
            }
            if(flag==1)break;
        }
        if(flag==0)printf("-1\n");
    }
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值