Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

 

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 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 Input

Input
4
2 1 3
2 4 2
Output
6 2
Input
3
1 2
2 1 3
Output
-1
题解:两个士兵打牌,按顺序出牌,若a的牌比b大,则把b的牌放到a的最后,再把a的这张牌放到b的那张排后面。很明显此题是队列问题可设置两个队列,按照题目意思模拟一下即可,注意在循环一定次数下仍然没有结果,则无解;另外就是可以直接标记状态搜索,可只标记第一个,也可以标记全部,但是自己写的有点乱,所以只介绍第一种了

#include<iostream>
#include<queue>
using namespace std;
const int N=;
queue <int>x,y;
int main()
{
int i,t,a,b,n,m;
cin>>t;
cin>>n;
for( i=; i<n; i++)
{
cin>>a;
x.push(a);
}
cin>>m;
for( i=; i<m; i++)
{
cin>>b;
y.push(b);
}
int s=;
while(!x.empty()&&!y.empty())
{
s++;
if(s>N) break;
int q=x.front();
int p=y.front();
x.pop();
y.pop();
if(q<p)
{
y.push(q);
y.push(p);
}
if(q>p)
{
x.push(p);
x.push(q);
}
}
if(x.empty())
cout<<s<<" "<<""<<endl;
else if(y.empty())
cout<<s<<" "<<""<<endl;
else cout<<"-1"<<endl; return ;
}

CodeForces 546C(队列)的更多相关文章

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

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

  2. 【codeforces 546C】Soldier and Cards

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. codeforces 704A (队列模拟) Thor

    题目:这里 题意:n个app,q个操作,当操作数type为1的时候表示y这个app推送了你一条消息,当操作数type为2的时候表示将y这个app已推送的所有消息都读完,当操作数为3的时候 表示将已经推 ...

  4. CodeForces 450A 队列

    Description There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let ...

  5. codeforces546C

    Soldier and Cards CodeForces - 546C Two bored soldiers are playing card war. Their card deck consist ...

  6. 「日常训练」 Soldier and Cards (CFR304D2C)

    题意 (Codeforces 546C) 按照指定的规则打牌,问谁胜或无穷尽. 分析 又是一条模拟,用set+queue(这里手写了)处理即可.注意到两种局势"1 234"和&qu ...

  7. CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列

    B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...

  8. Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列

    B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...

  9. Codeforces Beta Round #6 (Div. 2 Only) 单调队列

    题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...

随机推荐

  1. @protocol 和 category 中如何使用 @property

    出题者简介: 孙源(sunnyxx),目前就职于百度 整理者简介:陈奕龙(子循),目前就职于滴滴出行. 转载者:豆电雨(starain)微信:doudianyu 在 protocol 中使用 prop ...

  2. Python入门基础教程(儿童版) [分享一本入门级教程]

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1. 推荐书名 No Starch--Python for ...

  3. evernote出现"Invalid username and/or password"的情况

    evernote出现"Invalid username and/or password"的情况 evernote挺好用的,可是这几年用下来也遇到过狗血情况,几乎每次都是更新后出状况 ...

  4. Android CountDownTimer 倒计时

    摘自:http://www.cnblogs.com/over140/archive/2011/12/20/2294220.html 继承关系 public abstract class CountDo ...

  5. 字体图标 icon font

    Icon font icon font 指的是用字体文件代替图片文件,来展示图标.特殊字体等元素的方法. 应用场景: iconfont的优缺点 大小能够自由地变化 颜色能够自由地改动 加入阴影效果 * ...

  6. Scriptcase演示程序

    为了方便大家对Scriptcase有一个非常直观的印象,现在将Scriptcase的示例程序进行了适当汉化,并且放在互联网上,您可以直接点击这个地址访问. 需要说明的是,示例程序中提供的范例,基本上都 ...

  7. [io PWA] keynote: Launching a Progressive Web App on Google.com

    Mainly about Material design (effects / colors / flashy stuff) Components (web components / polymer) ...

  8. android控件上面实现提醒信息

    android开发中,经常会用到显示一个提醒信息,比如个人中心,有新信息,购买商品后,在购物车控件,显示购物数量等.我们可以用,2个控件来实现,或者用层叠图. 还有一种简单方便的办法,使用别人的开源代 ...

  9. Linux &amp; Mac curl 命令行使用——POST&amp;GET

    http提交一个表单,比較经常使用的是POST模式和GET模式 在curl的命令行下,GET模式什么option都不用.仅仅须要把变量写在url里面就能够了 比方: curl http://www.s ...

  10. Android系统移植与驱动开发——第七章——LED驱动

    LED驱动的实现原理 编写LED驱动: 测试LED驱动之前需要用USB数据线连接开发板,然后打开电源,成功启动之后,执行build.sh脚本文件编译和安装LED驱动,顺利则会自动连接 如果有多个设备文 ...