CF 546C(Soldier and Cards-deque)

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

deque:(双向队列)

push_back(a);

push_front(a);

pop_back();

pop_front();

q[a] 指向


题解:

本题11!种状态会T

但是大部分状态不可能出现,

于是卡时间即可。

(官方:给出的极限情况是106次,这游戏人类可玩。。)



#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<set>
#include<deque>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (10)
#define MAXStage (100000)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
deque<int> qa,qb; 
void work()
{
	int a=qa[0],b=qb[0];
	qa.pop_front();qb.pop_front();
	if (a<b) qb.push_back(a),qb.push_back(b);
	if (a>b) qa.push_back(b),qa.push_back(a);
		
	
}
set< pair< deque < int > , deque< int > > > h;
int n;
int main()
{
//	freopen("CF546C.in","r",stdin);
	cin>>n;
	int k1,k2;
	cin>>k1;
	For(i,k1)
	{
		int p;
		cin>>p;
		qa.push_back(p);
	} 
	cin>>k2;
	For(i,k2)
	{
		int p;
		cin>>p;
		qb.push_back(p);
	} 
	
	Rep(ans,MAXStage)
	{
		if (qa.empty()) 
		{
			printf("%d 2\n",ans);
			return 0;
		}
		else if (qb.empty())
		{
			printf("%d 1\n",ans);
			return 0;
		}
		else 
		{
			if (h.find(make_pair(qa,qb))!=h.end())
			{
				cout<<"-1"<<endl;
				return 0;
			}
			h.insert(make_pair(qa,qb));
			work();
	//		Rep(i,qa.size()) cout<<qa[i]<<' ';cout<<endl;
	//		Rep(i,qb.size()) cout<<qb[i]<<' ';cout<<endl;
			
			
		}
	}
	
	
	
	return 0;
}










你可能感兴趣的:(CF 546C(Soldier and Cards-deque))