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. 全栈JavaScript之路(七)学习 Comment 类型节点.

    凝视 在DOM中,用 Comment 类型 节点表示, 构造器函数为:  function Comment(){[native code]}. comment 节点的特征: nodeType:8 no ...

  2. CSS3媒体查询(Media Queries)

    媒体类型: all 所有设备 screen 电脑显示器 handheld 便携设备 tv 电视类型设备 print 打印用纸打印预览视图 关键字 and not(排除某种设备) only(限定某种设备 ...

  3. android api 中文 (75)—— AdapterView.OnItemClickListener

    前言 本章内容是android.widget.AdapterView.OnItemClickListener,版本为Android 2.3 r1,翻译来自"麦子",欢迎大家与他交流 ...

  4. CSS的float与clear

    首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流.如下图: 可以看出,即使div1的宽度很小,页面中一行可以容下div1和div2,div2也不会排在div1后边,因为d ...

  5. 使用IDENTITY列属性和Sequence对象

    使用IDENTITY列属性 1. 建立表 Sales.MyOrders USE TSQL2012; IF OBJECT_ID(N'Sales.MyOrders', N'U') IS NOT NULL ...

  6. 条款20:宁以pass-by-reference-to-const替换pass-by-value

    本条款的要点: 1.尽量以pass-by-reference-to-const替换pass-by-value.前者更高效且可以避免切割问题. 2.这条规则并不适用于内建类型及STL中的迭代器和函数对象 ...

  7. Android Activity和Intent机制学习笔记

    转自 http://www.cnblogs.com/feisky: Activity Android中,Activity是所有程序的根本,所有程序的流程都运行在Activity之中,Activity具 ...

  8. C++基础-位运算

    昨天笔试遇到一道题,让实现乘法的计算方法,设计方案并优化,后来总结位运算相关知识如下: 在计算机中,数据是以1010的二进制形式存储的,1bytes = 8 bits,bit就是位,所以位运算就是对每 ...

  9. Log4J logger图片

  10. android中使用DisplayMetrics获取屏幕参数

    --关于Density int android.graphics.Bitmap.getDensity(),返回bitmap-density(密度).默认的density就是当前display-dens ...