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. hdu 4739 Zhuge Liang's Mines (简单dfs)

    Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. linux进程间通信之信号

    1.wait()函数 原型:pid_t  wait(int *status) 子进程退出时,它向父进程发送一个SIGCHLD信号,默认情况是总是忽略SIGCHLD信号,此时进程状态一直保留在内存中,因 ...

  3. 字符串分割函数Demo

    #include <stdio.h> int getLength(char *string); int main(int argc, char **argv){ char str[12] ...

  4. SpringMVC请求访问不到静态文件解决方式

    如何你的DispatcherServlet拦截"*.do"这样的有后缀的URL,就不存在访问不到静态资源的问题. 如果你的DispatcherServlet拦截"/&qu ...

  5. Spark1.5.1的安装与部署 每一步详细测试截图

    转载或借鉴请注明转自 http://www.cnblogs.com/FG123/p/5101733.html  谢谢! 1.安装Spark之前需要先安装Java,Scala及Python(个人喜欢用p ...

  6. linux下检测端口是否连通

    检测tcp端口使用telnet命令 telnet 例:telnet 192.168.0.1 80 检测udp端口使用uc命令 uc -zu 例:uc -zu 192.169.0.1 80   以上命令 ...

  7. .vimrc快捷键设置

    $ cat ~/.vimrc,centos7是在/etc/vimrc文件中配置. nmap <C-_>s :cs find s <C-R>=expand("<c ...

  8. kinect for windows - DepthBasics-D2D详解之一

    Depth在kinect中经常被翻译为深度图,指的是图像到摄像头的距离,这些距离数据能让机器知道物理距离有多远.kinect通过两个红外摄像头来实现这个功能的.在这个例子里,就实现了深度图的提取和现实 ...

  9. CocoaPods的安装及设置

    1>CocoaPods简介 CocoaPods是一个用来帮助我们管理第三方依赖库的工具 在开发iOS应用时,会经常使用第三方类库,手动下载比较麻烦,通过CocoaPods可以便捷的下载与管理第三 ...

  10. linux命令--sysctl

    sysctl sysctl被用来在执行时配置内核参数.这些参数都存储在/proc/sys/(以键-值对形式存储)中.你可以用sysctl来读和写数据 命令参数 variable   要读的键值的名字 ...