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

Hint

First sample:

Second sample:

题意:

两个人打牌,共有n张各不相同的牌,两人分别有k1和k2张,每人从牌堆顶取一张牌对比,牌大的一方分别将对方牌放入牌堆底,再将自己出的牌放入牌堆底。如此进行直到某一方牌堆为空。如果不能结束则输出-1.

典型的队列应用嘛~说来惭愧队列的基本操作都忘得差不多了,做题时还腆着脸地去看了一下以前写的博客。。

按照要求入队出队就可以了,还有需要注意的就是开始输入时的顺序,千万不要先将k1、k2输完再循环输入各自的牌值。

还有一件事,判断死循环的话实在是太麻烦了!!!在此处我取了个巧,将循环次数>100000全部判定为死循环,意料之中的A了 ~  不要打我【抱头

附AC代码:

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; int main(){
int n,k1,k2,x;
cin>>n>>k1;//注意是k1、k2分开输
queue<int> l;
queue<int> r;
for(int i=;i<k1;i++){//入队
cin>>x;
l.push(x);
}
cin>>k2;
for(int i=;i<k2;i++){
cin>>x;
r.push(x);
}
//cout<<r.front()<<" "<<l.front()<<endl;
int ans=;
while(){
if(l.front()>r.front()){//当1大时按顺序分别将r的队顶元素和l的队顶元素插入l队尾
l.push(r.front());
l.push(l.front());
l.pop();//注意弹出已插入的元素
r.pop();
ans++;
}
else if(l.front()<r.front()){//同上
r.push(l.front());
r.push(r.front());
r.pop();
l.pop();
ans++;
}
if(l.empty()){//若l为空时,2 win
cout<<ans<<" "<<""<<endl;
return ;
}
if(r.empty()){
cout<<ans<<" "<<""<<endl;
return ;
}
if(ans==){//取巧了 哈哈
cout<<"-1"<<endl;
return ;
}
}
return ;
}

最新文章

  1. Android(3)—Mono For Android App版本自动更新(2)
  2. 性能测试工具 转自https://yq.aliyun.com/articles/35149?spm=5176.100239.blogcont35147.8.rsow6k
  3. vs文件属性(生成操作)各选项功能(发布Web项目时使用)
  4. Ubuntu修改文件关联
  5. Ubuntu 设置程序开机启动(以指定用户身份)
  6. 几种常用远程通信技术(RPC,Webservice,RMI,JMS)的区别
  7. 玩转sublime(一)——玩转全局文件搜索/替换
  8. TSS 任务状态段
  9. 《Journey》风之旅人;
  10. 一个可视化的retrospective网站
  11. position: absolute;绝对定位水平居中问题
  12. Nullable&lt;T&gt; 与 T?
  13. 浅谈SQL优化入门:1、SQL查询语句的执行顺序
  14. servlet_2
  15. vue的动态路由(登录之后拿到动态路由通过addRouters()动态添加路由)
  16. sql 一对多查询
  17. C# 编写WCF简单的服务端与客户端
  18. sql开启远程访问
  19. 基于Docker一键部署大规模Hadoop集群及设计思路
  20. TED_Topic9:How we're priming some kids for college &mdash; and others for prison

热门文章

  1. axis2调用WSDL接口
  2. 在matlab中对中国地图中的不同省份按照高度进行渲染
  3. mysql explain 的type解释
  4. IOS --关于粘贴板 ,剪切板 ,UILabel的复制
  5. 使用母版页时内容页如何使用css和javascript
  6. 经典游戏“大富翁4”存档文件修改器Rich4Editor下载
  7. react 创建组件 (三)PureComponet
  8. RESTful Web Service
  9. 走入asp.net mvc不归路:[4]说说Action有哪些常见成员
  10. 获取DOM父元素和子元素