您的位置:首页 > 其它

CF 546C Soldier and Cards

2015-10-26 22:23 232 查看
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 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 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


题目意思是两个人玩牌,首先两个人都拿出自己手牌的最上面的进行拼点,两张拼点牌将都给拼点赢得人,这两张牌放入手牌的顺序是:先放对方的牌再放自己的。若最后有一个人没有手牌了,那么他就输了,求输出拼点的次数和赢得人的编号,如果一直无法结束比赛,则输出-1.

#include<iostream>
#include<queue>
using namespace std;
queue<int>q1;
queue<int>q2;
int main()
{
int n;
cin >> n;
int n1;
cin >> n1;
int a;
for(int i = 0;i<n1;i++)
{
cin >> a;
q1.push(a);
}
int n2;
cin >> n2;
for(int i = 0;i<n2;i++)
{
cin >> a;
q2.push(a);
}
int ans = 0;
while(ans<40000&&!q1.empty()&&!q2.empty())
{
int v1 = q1.front();
q1.pop();
int v2 = q2.front();
q2.pop();
if(v1>v2)
{
q1.push(v2);
q1.push(v1);
}
else
{
q2.push(v1);
q2.push(v2);
}
ans++;
}
if(ans==40000)cout<<-1<<endl;
else
{
if(q1.empty())cout<<ans<<" "<<2<<endl;
else cout<<ans<<" "<<1<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: