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.

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:

Second sample:


解题说明:此题是一道模拟题,有两个人,每个人又一些牌,总张数不超过10,每张牌都有个不同价值,每个人的牌的标号固定的,出牌只能选标号最小的出,然后比较大小,大的那个获胜,得到对方的牌,并把获得的这张牌与自己出的那张牌,都放到尾部,然后如此循环,最后没有手牌的人输掉比赛。直接用数组记录进行模拟即可。


#include<stdio.h>
#include <string.h>
#include<iostream>

using namespace std;

#define ll long long

int a[10000000],b[10000000],s,n,la,lb,ia,ib;

int main()
{ 
	cin>>n;
	cin>>la; 
	for(int i=1;i<=la;i++) 
	{
		cin>>a[i];
	}
	cin>>lb; 
	for(int i=1;i<=lb;i++) 
	{
		cin>>b[i];
	}
	ia=ib=1;
	while (ia<=la&&ib<=lb&&la<(1<<20)) 
	{
		s++;
		if (a[ia]>b[ib]) 
		{
			a[++la]=b[ib]; 
			a[++la]=a[ia];
		} 
		else 
		{
			b[++lb]=a[ia]; 
			b[++lb]=b[ib];
		}
		ia++;
		ib++;
	}
	if (la>=(1<<20)) 
	{
		cout<<-1<<endl; 
	}
	else 
	{ 
		cout<<s<<' '; 
		if (ia>la) 
		{
			cout<<2<<endl; 
		}
		else 
		{	
			cout<<1<<endl;
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值