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

Sample Output

1 7 19 1 19 37
 
分析:
   将两副最顶端的牌进行比较,小的的放到大的那副牌的下面,大的又放到小的下面,其实就是先进先出问题,所以用队列。
 
 
 
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int main()
{
int n,k1,k2,x;
int flag=0,kase=0, win;
while(scanf("%d",&n)==1&&n)
{
queue<int>q1,q2;
scanf("%d",&k1);
for(int i=0;i<k1;i++)
{
scanf("%d",&x);
q1.push(x);
}
scanf("%d",&k2);
for(int i=0;i<k2;i++)
{
scanf("%d",&x);
q2.push(x);
}
while(n)
{
int y;
x=q1.front();
y=q2.front();
q1.pop();
q2.pop();
kase++;
if(kase==1e3)
break;
if(x>y)
{
q1.push(y);
q1.push(x);
}
else if(x<y)
{
q2.push(x);
q2.push(y);
}
if(q1.size()==0||q2.size()==0)
{
win=q1.empty()?2:1;
flag=1;
break;
} }
if(flag)
printf("%d %d\n",kase,win);
else
printf("-1\n");
}
return 0;
}

Problem B 队列的更多相关文章

  1. Wannafly 挑战赛 19 参考题解

    这一次的 Wannafly 挑战赛题目是我出的,除了第一题,剩余的题目好像对大部分算法竞赛者来说好像都不是特别友好,但是个人感觉题目质量还是过得去的,下面是题目链接以及题解. [题目链接] Wanna ...

  2. hdu 3706 Second My Problem First 单调队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3706 Second My Problem First Time Limit: 12000/4000 M ...

  3. HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/500 ...

  4. HDU 6319.Problem A. Ascending Rating-经典滑窗问题求最大值以及COUNT-单调队列 (2018 Multi-University Training Contest 3 1001)

    2018 Multi-University Training Contest 3 6319.Problem A. Ascending Rating 题意就是给你长度为k的数列,如果数列长度k<n ...

  5. HDU 6319 Problem A. Ascending Rating(单调队列)

    要求一个区间内的最大值和每次数过去最大值更新的次数,然后求每次的这个值异或 i 的总和. 这个序列一共有n个数,前k个直接给出来,从k+1到n个数用公式计算出来. 因为要最大值,所以就要用到单调队列, ...

  6. 循环队列(Joseplus Problem)

    #include <iostream> #include <stdio.h> using namespace std; ]; ; void Enqueue(int x) { ) ...

  7. 2016集训测试赛(二十)Problem A: Y队列

    Solution 考虑给定一个\(n\), 如何求\(1\)到\(n\)的正整数中有多少在队列中. 不难注意到我们只需要处理质数次方的情况即可, 因为合数次方会被其因数处理到. 同时我们考虑到可能存在 ...

  8. Second My Problem First HDU - 3706 单调队列

    单调队列 单调队列是指一个队列内部的元素具有严格单调性的一种数据结构,分为单调递增队列和单调递减队列. 单调队列满足两个性质 1.单调队列必须满足从队头到队尾的严格单调性. 2.排在队列前面的比排在队 ...

  9. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

随机推荐

  1. 【CDN】国外访问国内服务器网站-响应慢-CDN

    建议采用CDN海外加速方式: (1)CDN即内容分发网络(Content Delievery Network),它可以认为是建立在现有IP网络基础结构之上的一种增值网络.CDN技术将多点负载均衡.镜像 ...

  2. Hbase之更新单条数据

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...

  3. 抛弃vboot不格盘用grub4dos+firadisk安装Ghost版XP到VHD,轻松RAMOS!

    http://bbs.wuyou.net/forum.php?mod=viewthread&tid=363198&extra=抛弃vboot不格盘用grub4dos+firadisk安 ...

  4. linux笔记:RPM软件包管理-yum在线管理

    ip地址配置: 用ifconfig命令只能配置ip和子网掩码,这样只能访问内网:如果需要访问公网则还必须要网关和DNS. 使用setup工具配置ip: 网络yum源配置: 常用yum命令:查询 常用y ...

  5. js简单模仿队列

    window.meng = window.meng || {}; (function () { var items = []; meng.queue = { /** * * @param {Funct ...

  6. nodeschool.io 8

    ~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...

  7. SAP连接HANA数据库

    既然都用HANA了,为什么还要在SAP端,连接HANA数据库,做数据库处理..... 因为HANA数据库,没个用户在STADIO上建的数据库表...只能这个用户使用,而做Universe 设计的时候, ...

  8. 多线程相关Interlocked.Increment问题

    今天群里有人问到如下代码打印出来的东西为什么不是连续得,所以有大神解释了原因.在这过程中遇到了些奇怪的情况 static void Main(string[] args) { for (int i = ...

  9. linux apache 自动监护脚本

    1 首先安装curl yum install curl 2 编写shell vi restart_apache.sh 写入一下内容 #!/bin/bashURL="http://127.0. ...

  10. mysql在一台服务器搭建主从

    注:本环境事先执行rm -rf /usr/local/mysql   以方便实验. 1. 主与从,类似于A机器和B机器的连接,通过bin_log和rpel_log 进行数据连接 2. 如图所示: 3. ...