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
程序分析:两个人每个人都有一堆牌,他们每个人从他那堆拿出最上面的牌,并放在桌子上。

牌值更大的那个先他的对手的牌他的牌的底部,然后他把他的卡片放他牌的底部,

如此循环。如果有一个玩家的一个堆栈为空,他输了,另一个胜利。

思路:

将两个人的牌值分别放入不同的队列,后队首与队首进行比较,将值小的那队的对首放入,另一队的队尾再将值大的那个队的队首放队尾。

将两个队的队首都删除。如此循环直到其中一个队为空时跳出循环。

程序代码

#include <iostream>
#include <queue>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int k1,k2,x,time=,y,victory,flag;
queue<int> q1;
queue<int> q2;
cin>>k1;
for(int i=; i<k1; i++)
{
cin>>x;
q1.push(x);
}
cin>>k2;
for(int i=; i<k2; i++)
{
cin>>x;
q2.push(x);
}
while()
{
flag=;
x=q1.front();
y=q2.front();
q2.pop();
q1.pop();
time++;
if(time==1e4)
break;
if(x>y)
{
q1.push(y);
q1.push(x);
}
else
{
q2.push(x);
q2.push(y);
}
if(q1.size()==||q2.size()==)
{
victory=(q1.size()==?:);
flag=;
break;
} }
if(flag)
cout<<time<<" "<<victory<<endl;
else
cout<<-<<endl;
}
return ;
}

B - 队列,推荐的更多相关文章

  1. RabbitMQ快速入门

    最近一段项目实践中大量使用了基于RabbitMQ的消息中间件,也积累的一些经验和思考,特此成文,望大家不吝赐教. 本文包括RabbitMQ基本概念.进阶概念.实践与思考等三部分,着重强调相关概念和基于 ...

  2. MySQL优化小结

    数据库的配置是基础.SQL优化最重要(贯穿始终,每日必做),由图可知,越往上优化的面越小,最基本的SQL优化是最重要的,往上各个参数也没太多调的,也不可能说调一个innodb参数性能就会好多少,而动不 ...

  3. 为什么阿里巴巴要禁用Executors创建线程池?

    作者:何甜甜在吗 juejin.im/post/5dc41c165188257bad4d9e69 看阿里巴巴开发手册并发编程这块有一条:线程池不允许使用Executors去创建,而是通过ThreadP ...

  4. 为什么尽量不要使用Executors创建线程池

    看阿里巴巴开发手册并发编程这块有一条:线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,通过源码分析禁用的原因. 线程池的优点 管理一组工作线程,通过线程池 ...

  5. [转]为什么阿里巴巴要禁用Executors创建线程池?

    作者:何甜甜在吗 链接:https://juejin.im/post/5dc41c165188257bad4d9e69 来源:掘金 看阿里巴巴开发手册并发编程这块有一条:线程池不允许使用Executo ...

  6. 老李推荐:第6章6节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-命令队列

    老李推荐:第6章6节<MonkeyRunner源码剖析>Monkey原理分析-事件源-事件源概览-命令队列   事件源在获得字串命令并把它翻译成对应的MonkeyEvent事件后,会把这些 ...

  7. Python 3 并发编程多进程之队列(推荐使用)

    Python 3 并发编程多进程之队列(推荐使用) 进程彼此之间互相隔离,要实现进程间通信(IPC),multiprocessing模块支持两种形式:队列和管道,这两种方式都是使用消息传递的. 可以往 ...

  8. 消息队列面试题、RabbitMQ面试题、Kafka面试题、RocketMQ面试题 (史上最全、持续更新、吐血推荐)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  9. 各消息队列对比,Kafka深度解析,众人推荐,精彩好文!

    http://blog.csdn.net/allthesametome/article/details/47362451

随机推荐

  1. C功底挑战Java菜鸟入门概念干货(二)

    (接上篇博文:C功底挑战Java菜鸟入门概念干货(一)) 一.Java面向对象程序设计-类的基本形式 1.“类”是把事物的数据与相关的功能封装在一起,形成的一种特殊结构,用以表达对真实世界的一种抽象概 ...

  2. DOC下编译和运行带有包的java类文件

    前言: 带有包名的java类在DOC下编译可以成功,但是运行出错  错误: 找不到或无法加载主类 com.soanl.socket.MyServer D盘temp文件下有个Hello.java文件,包 ...

  3. hdu 3228 (最大流+二分)

    题意:一共有N个城市,一些城市里有金矿,一些城市里有仓库,金矿和仓库都有一个容量,有M条边,每条边是双向的,有一个权值,求将所有金矿里的储量都运送到仓库中,所需要经过的道路中,使最大的权值最小 思路: ...

  4. srm 534

    250 Description 给你一个1*n的棋盘.两人轮流行动,每一个人能够把"o"向右移动到空格子.或者跨越连续两个"o"到空格子. 一个"o& ...

  5. js正则表达式验证字符长度

    原理,就是把一个汉字替换为两个字母,来实现长度验证. //js正则验证字符长度 第一种:直接输出长度 alert('1sS#符'.replace(/[^\x00-\xff]/g, 'AA').leng ...

  6. flash builder 4 编译器参数

    accessible=true|false 是否具有可理解性(如为残疾人提供方便的性能) actionscript-file-encoding | 设置文件编码,如Shitf_JIS advanced ...

  7. python命令行解析工具argparse模块【4】

            上一节我们讲解了add_argument()方法,这一节我们将学习parse_args()方法.          parse_args()方法的作用是解析命令行参数,并返回解析之后的 ...

  8. r语言之条件、循环语句

    if条件语句:if (conditon) {expr1} else {expr2} > x<-1> if(x==1)+ {x<-"x=1"}else+ {x ...

  9. BZOJ 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场

    题目 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 491  S ...

  10. JS获取中文拼音首字母,并通过拼音首字母高速查找页面内的中文内容

    实现效果: 图一: 图二: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGVzdGNzX2Ru/font/5a6L5L2T/fontsize/400/f ...