Soldier and Cards

CodeForces - 546C

Two bored soldiers are playing card war. Their card deck consists of exactly ncards, 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.

Examples

Input
4
2 1 3
2 4 2
Output
6 2
Input
3
1 2
2 1 3
Output
-1

sol: 想了半天也没什么好方法,这个数据范围这么小(n<=10),直接暴力模拟,如果次数超过500就puts("-1")
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
int n;
queue<int>Q1,Q2;
int main()
{
int i,ans=;
R(n);
R(n); while(n--) Q1.push(read());
R(n); while(n--) Q2.push(read());
while((!Q1.empty())&&(!Q2.empty()))
{
int a=Q1.front(),b=Q2.front();
if((++ans)>) break;
if(a>b)
{
Q1.push(b);
Q1.push(a);
}
else
{
Q2.push(a);
Q2.push(b);
}
Q1.pop(); Q2.pop();
}
if(Q1.empty())
{
W(ans); Wl();
}
else if(Q2.empty())
{
W(ans); Wl();
}
else puts("-1");
return ;
}
/*
input
4
2 1 3
2 4 2
output
6 2 input
3
1 2
2 1 3
output
-1
*/
 

codeforces546C的更多相关文章

  1. 【CodeForces - 546C】Soldier and Cards (vector或队列)

    Soldier and Cards 老样子,直接上国语吧  Descriptions: 两个人打牌,从自己的手牌中抽出最上面的一张比较大小,大的一方可以拿对方的手牌以及自己打掉的手牌重新作为自己的牌, ...

随机推荐

  1. Unity编辑器:清空控制台(Console)

    static MethodInfo clearMethod = null; /// <summary> /// 清空log信息 /// </summary> private s ...

  2. kafka模型理解

    1.消息发送至一个topic,而这个topic可以由多个partition组成,每条消息在partition中的位置称为offset 2.消息存在有效期,如果设置为2天,则消息2天后会被删除 3.每个 ...

  3. Linux 特殊权限 SUID,SGID,SBIT

    setuid 和 setgid 分别是 set uid ID upon execution 和 set group ID upon execution 的缩写.我们一般会再次把它们缩写为 suid 和 ...

  4. 新浪2017校园招聘---C++后台研发

    一共10道题目,难度不大,就是题量大,时间短. 1.  编程        写一个函数,求出一字符串的所有排列. 2.  编程        实现一个在32位系统下把字符串转换成浮点数的函数 floa ...

  5. Magic Stones CodeForces - 1110E (思维+差分)

    E. Magic Stones time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. iOS- 利用AFNetworking(AFN) - 实现文件断点下载

    https://www.cnblogs.com/qingche/p/3500746.html 1. 定义一个全局的AFHttpClient:包含有 1> baseURL 2> 请求 3&g ...

  7. Pair Project

    以前只是一个人完成一个项目,不论什么都是,现在突然要两个人一起来写, 听上去挺稀奇的,也挺简单的,可惜了就是“听上去”而已.我认为这也是一种技术啊~ 我跟我的搭档研究了好久好久,选择了好久,然后也选了 ...

  8. 学习memcache

    本文参考了菜鸟教程中的内容. 安装 安装memcache的时候,请切换为root用户 root@centos # wget http://www.memcached.org/files/memcach ...

  9. tomcat7 server.xml max thread

    java - Tomcat - maxThreads vs maxConnections - Stack Overflowhttps://stackoverflow.com/questions/246 ...

  10. java不同的包下相同的类名的问题与解决办法

    Java中的类以包进行分类组织,当程序中需要用到某个包下的类时,可以以该类的全限定名进行引用.这样,不同的包中的类就可以同名,不会产生混淆. 但是这样就可能导致引用的时候会产生一些问题. 第一个问题, ...