您的位置:首页 > 其它

CodeForces-546C-Soldier and Cards

2016-04-14 09:37 218 查看
Description

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all 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 thisfight 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 followk1 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 offights before end of game and the second one is1 or 2 showing which player has won.

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

Sample Input

Input

4

2 1 3

2 4 2

Output

6 2

Input

3

1 2

2 1 3

Output

-1

Hint

First sample:

Second sample:

有两个人,每个人又一些牌,总张数不超过10,每张牌都有个不同价值,每个人的牌的标号固定的,出牌只能选标号最小的出,然后比较大小,大的那个获胜,得到对方的牌,并把获得的这张牌与自己出的那张牌,都放到尾部,然后如此循环,最后没有手牌的人输掉比赛

我一看拿前面的,放到后面就直接想到用队列模拟了,然后随便设置了一个次数,没想到对了,但是队列清空那个地方我卡了很久

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;

const int N=1000000;
queue <int>va,vb;

int main()
{
int t,a,b,n,m;
while(scanf("%d",&t)!=EOF)
{
while(va.size())va.pop();
while(vb.size())vb.pop();
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d",&a);
va.push(a);
}
scanf("%d",&m);
for(int i=0; i<m; i++)
{
scanf("%d",&b);
vb.push(b);
}
int s=0;
while(!va.empty()&&!vb.empty())
{
s++;
if(s>N) break;
int vah=va.front();
int vbh=vb.front();
va.pop();
vb.pop();
if(vah<vbh)
{
vb.push(vah);
vb.push(vbh);
}
if(vah>vbh)
{
va.push(vbh);
va.push(vah);
}
}
if(va.empty()||vb.empty())
{
if(va.empty())
{
printf("%d 2\n",s);
}
else printf("%d 1\n",s);
}
else puts("-1");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: