题干:

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.

Examples

Input

4
2 1 3
2 4 2

Output

6 2

Input

3
1 2
2 1 3

Output

-1

Note

First sample:

Second sample:

题目大意:

两个人玩纸牌游戏,每一轮游戏每个人都把自己的第一个牌拿出来比较大小,大的一方先将对面的这个牌放在自己牌的最后,再把自己的牌放在最后,直到一个人没有了纸牌就算那个人输,问一共会进行多少次游戏,以及赢的人的编号。如果决不出胜负那就输出-1

解题报告:

模拟就行了。猜他最多不超过1e7次、、其实这题的上界是4e7次,因为你想啊,最多的排列可能是n!种,然后分到两个人手中,可能每个人手中的牌有(0,n)(1,n-1)...(n,0)共(n+1)中可能性,所以一共的局面有(n+1)!种情况,算一下最多4e7左右。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#include<ctime>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n,k1,k2;
deque<int> a,b;
int main()
{cin>>n;cin>>k1;for(int x,i = 1; i<=k1; i++) {scanf("%d",&x);a.pb(x);}cin>>k2;for(int x,i = 1; i<=k2; i++) {scanf("%d",&x);b.pb(x);}if(k1 == 0) return 0 * printf("0 2\n");if(k2 == 0) return 0 * printf("0 1\n");int i;int flag = 0;for(i = 1; i<=10000000; i++) {int aa = a.front();a.pop_front();int bb = b.front();b.pop_front();if(aa > bb) {a.pb(bb);a.pb(aa);}else {b.pb(aa);b.pb(bb);}if(a.empty() || b.empty()) {flag = 1;break;}//printf("a : %d b : %d\n",a.size(),b.size());}if(a.empty()) {printf("%d %d\n",i,2);}if(b.empty()) {printf("%d %d\n",i,1);}if(i > 10000000) printf("-1\n");return 0 ;}

【CodeForces - 546C 】Soldier and Cards (模拟)相关推荐

  1. CodeForces - 546C Soldier and Cards(模拟)

    题目链接:点击查看 题目大意:两个人在玩游戏,初始时两个人分别有一定数量的牌,牌面的大小一定是互不相同的,游戏规则如下: 每次两个人同时从自己牌堆的最顶端取出一张牌,我们可以记做a和b,比较一下其大小 ...

  2. CodeForces 1463 C. Busy Robot 模拟

    CodeForces 1463 C. Busy Robot 模拟 题目大意: 有一个一维坐标轴,在最初时刻有个机器人位于坐标 0 0 0 位置,有 n n n 个命令,对于每一个命令在 t i t_i ...

  3. CF546C. Soldier and Cards(队列+模拟)

    题目链接:https://acm.njupt.edu.cn/problem/CF546C/editor 解题思路:唯一难点就是如何判断能不能分出胜负,这里因为总卡片不大于10,所以直接判断在队列进行次 ...

  4. BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...

  5. 【CodeForces Round #550】A-F | 模拟 | 贪心 | 高精 | BFS | 二分图 | E

    今年怎么没有愚人节比赛了   CF你看看人家洛谷   唉鸭原来那边还没到愚人节呢- 愚人节比赛还是有的,在今晚 qwq [CodeForces 1144   A-F] Tags:模拟 贪心 BFS 高 ...

  6. CodeForces - 1328F Make k Equal(模拟)

    题目链接:点击查看 题目大意:给出一个数列 a ,现在有两种操作: 找到一个最小值,使其值加一 找到一个最大值,使其值减一 注意这里找到一个最值进行的操作,是针对最值不唯一的情况,题目问至少需要进行多 ...

  7. CodeForces - 1321C Remove Adjacent(贪心+模拟)

    题目链接:点击查看 题目大意:给出一个长度不超过100且只包含小写字母的字符串,现在规定,如果某个位置 i 的相邻位置存在着当前位置所代表字母的前一个字母,即 i - 1 和 i + 1 中存在着 a ...

  8. CodeForces - 1208E Let Them Slide(模拟+multiset)

    题目链接:点击查看 题目大意:给出n个数列,每行放一个,现在指定一个宽度w,满足w不小于n个数列中最长的那个数列的长度,现在可以将n个数列都放入到一个n*w的矩形之中,每个数列可以在各自的行内左右移动 ...

  9. CodeForces - 224C. Bracket Sequence (栈模拟)简单做法

    A bracket sequence is a string, containing only characters "(", ")", "[&quo ...

最新文章

  1. 如果有一天 Pytorch / Tensorflow 不开源了,我们该怎么办?
  2. SpringBoot报错:Could not autowire. No beans of ‘DiscussantMapper‘ type found
  3. spring boot 拦截器获取controller返回的数据_高级码农Spring Boot实战与进阶之过滤器和拦截器的使用及其区别...
  4. Windows 命令行大全
  5. 特征筛选4——斯皮尔曼相关系数筛选特征(单变量筛选)
  6. deepin php docker,Deepin15.10安装Docker
  7. android进程守护 失效,保持Service不被Kill掉的方法--双Service守护 Android实现双进程守护 1...
  8. Redis进程异常退出排查
  9. 有趣 的java代码_[分享]几段有趣的JAVA代码
  10. gdiPlus 显示图片缩放不正确的可能解决方案
  11. 如何通过网页获取该网站的js框架
  12. ask调制流程图_利用卷积神经网络的自动调制分类算法
  13. ReactOS学习笔记--编译和调试
  14. Java小程序:个人所得税计算(与标准个税有差距)
  15. echarts折线图设置横向基准线/水平线,超过基准线时折线会变色
  16. 如何解决flex:1撑开父元素问题
  17. 0x800700c1添加语言,win10检查更新失败,错误代码 0x800700c1
  18. 什么是BFC?BFC的原理是什么?如何创建BFC?
  19. 苹果邮箱怎么登录qq邮箱_gmail邮箱登录官网方法
  20. 校园创业项目有哪些?

热门文章

  1. Java学习笔记10-2——MyBatis
  2. Tidb集群加mysql_TiDB - 快速入门,集群搭建
  3. 实例化Java对象_Java面向对象基础之对象实例化
  4. 2-10 [搞定!]出栈序列的合法性 (20 分)
  5. 前端vue适配不同的分辨率_前端面试时,被问到项目中的难点有哪些?
  6. DOM Element对象的offsetXXX方法
  7. 腾讯视频客户端导出MP4格式
  8. python turtle 绘图速度用函数会快吗_有趣的Python turtle绘图
  9. 解决VS命令提示符 “Setting environment for using Microsoft Visual Studio. 此时不应有“系列错误
  10. ELF文件和BIN文件