【CodeForces - 546C 】Soldier and Cards (模拟)

题干:

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 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440709.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux wifi 蓝牙冲突,linux 下 无线 wifi 蓝牙 无法启用

linux 下 无线 wifi 蓝牙 无法启用装了Debian squeeze 后发现无线不能打开首先想到的是装驱动于是在wiki.debian.org上查了下以重新装了下驱动#aptitude install firmware-b43-installler#modprobe b43# iwconfiglo no wireless extensions.eth0 no wireless exten…

Linux中wait接口用于延时,linux2.6驱动编写参考

1、 使用新的入口必须包含 module_init(your_init_func);module_exit(your_exit_func);老版本&#xff1a;int init_module(void);void cleanup_module(voi);2.4中两种都可以用&#xff0c;对如后面的入口函数不必要显示包含任何头文件。2、 GPLMODULE_LICENSE("Dual BSD/…

【51nod - 1108】距离之和最小 V2(曼哈顿距离,中位数性质)

题干&#xff1a; 三维空间上有N个点, 求一个点使它到这N个点的曼哈顿距离之和最小&#xff0c;输出这个最小的距离之和。 点(x1,y1,z1)到(x2,y2,z2)的曼哈顿距离就是|x1-x2| |y1-y2| |z1-z2|。即3维坐标差的绝对值之和。 收起 输入 第1行&#xff1a;点的数量N。(2 <…

Linux实验室阿里云证书,开发者云体验实验室

{"data":[{"title":"技术领域","data":[{"title":"全部","key":1,"children":[{"title":"程序语言","key":12,"children":[{"title":&qu…

【OpenJudge - noi - 7624】山区建小学(dp)

题干&#xff1a; 总时间限制: 1000ms 内存限制: 65536kB 描述 政府在某山区修建了一条道路&#xff0c;恰好穿越总共m个村庄的每个村庄一次&#xff0c;没有回路或交叉&#xff0c;任意两个村庄只能通过这条路来往。已知任意两个相邻的村庄之间的距离为di&#xff08;为…

nuc8i7beh安装linux随机重启,【图片】来分享一下我的NUC8I7BEH【intelnuc吧】_百度贴吧...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼测试来了&#xff0c;Linux 脚本&#xff0c;2G测试&#xff0c;操完这次以后休息俩礼拜。ssd随机读4kfio -filename./ran4K.log -direct1 -iodepth 1 -thread -rwrandread -ioenginepsync -bs4k -size2G -numjobs10 -runtime1000 …

【Codeforces - 找不到题号】三元环计数(bitset优化,压位)

题干&#xff1a; 给你一个二维字符矩阵&#xff0c;如果 ( i , j ) 为 表明 两点之间有一条有向边&#xff0c;为-表示没有边&#xff0c;那么你要找出所有的三元环的个数。顶点数N<1500。 解题报告&#xff1a; 考虑最暴力的方法&#xff0c;开个二维数组来存每两个顶点之…

自定义函数删除字母C语言,[编程入门]自定义函数之字符提取-题解(C语言代码)...

解题思路:输入一个字符串&#xff0c;调用函数&#xff0c;遍历字符串中每一个字符&#xff0c;看是否含有aeiou字符&#xff0c;若有&#xff0c;将其保存到另一个字符型数组中&#xff0c;在主函数中对得到的字符型数组进行排序&#xff0c;输出。注意事项:题目要求顺序输出元…

【Hihocoder - offer编程练习赛39 - D】前缀后缀查询(后缀字典树,哈希,思维)

题干&#xff1a; 时间限制:10000ms 单点时限:1000ms 内存限制:512MB 描述 给定一个包含N个单词的字典:{W1, W2, W3, ... WN}&#xff0c;其中第i个单词Wi有具有一个权值Vi。 现在小Hi要进行M次查询&#xff0c;每次查询包含一个前缀字符串Pi和一个后缀字符串Si。他希望…

c 语言 while break,26 C 语言中的break和continue - C 语言基础教程

循环语句很好用&#xff0c;但是如果循环进行到一般想要跳出循环或者结束循环怎么办&#xff1f;那么那你需要 break 和 continue 语句。1. break 和 continue 的使用语法1.1 or 循环中使用 break 和 continuebreakfor (控制循环的变量; 循环判断条件; 循环变量增减变化){语句1…

【牛客 - 21302】被3整除的子序列(线性dp)

题干&#xff1a; 给你一个长度为50的数字串,问你有多少个子序列构成的数字可以被3整除 答案对1e97取模 输入描述: 输入一个字符串&#xff0c;由数字构成&#xff0c;长度小于等于50 输出描述: 输出一个整数 示例1 输入 复制 132 输出 复制 3 示例2 输入 复制 …

c语言链表实现数组逆置,数组与链表等顺序表逆置

一)数组的逆置(1)算法#indclude#define N 8main(){int array[N] {100,90,80,70,60,50,50,40};int i,j,t;for(i0,jN-1;i{t array[i];array[i] array[j];array[j] t;}for(i0,iprintf("%d",qlist.data[i])}(2)时间复杂度由于只需循环N/2即可完成逆置,所以时…

linux系统get命令详解,Ubuntu Linux系统下apt-get命令详解

整理了Ubuntu Linux操作系统下apt-get命令的详细说明,分享给大家。常用的APT命令参数&#xff1a;apt-cache search package 搜索包apt-cache show package 获取包的相关信息&#xff0c;如说明、大小、版本等sudo apt-get install package 安装包sudo apt-get install package…

*【Hihocoder - offer编程练习赛94 - A】最短管道距离(中位数)

题干&#xff1a; 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在一张2D地图上有N座城市&#xff0c;坐标依次是(X1, Y1), (X2, Y2), ... (XN, YN)。 现在H国要修建一条平行于X轴的天然气主管道。这条管道非常长&#xff0c;可以认为是一条平行于X轴的直线。…

android开发百度地图坐标偏差,利用百度地图Android sdk高仿微信发送位置功能及遇到的问题...

接触了百度地图开发平台半个月了&#xff0c;这2天试着模仿了微信给好友发送位置功能&#xff0c;对百度地图的操作能力又上了一个台阶我在实现这个功能的时候&#xff0c;遇到一些困难&#xff0c;可能也是别人将会遇到的困难&#xff0c;特在此列出1、在微信发送功能中&#…

*【牛客 1 - A】矩阵(字符串hash)

题干&#xff1a; 给出一个n * m的矩阵。让你从中发现一个最大的正方形。使得这样子的正方形在矩阵中出现了至少两次。输出最大正方形的边长。 输入描述: 第一行两个整数n, m代表矩阵的长和宽&#xff1b; 接下来n行&#xff0c;每行m个字符&#xff08;小写字母&#xff…

*【洛谷 - P1025】数的划分(dfs 或 dp 或 母函数,第二类斯特林数Stirling)

题干&#xff1a; 题目描述 将整数n分成k份&#xff0c;且每份不能为空&#xff0c;任意两个方案不相同(不考虑顺序)。 例如&#xff1a;n7&#xff0c;k3&#xff0c;下面三种分法被认为是相同的。 1,1,5 1,5,1 5,1,1 问有多少种不同的分法。 输入输出格式 输入格式&am…

kali linux 截图 软件,Kali-Linux-Tools-Interface:针对Kali Linux的图形化Web接口

Kali-Linux-Tools-Interface在当今这个信息时代&#xff0c;数据是最有价值的资产&#xff0c;因此&#xff0c;广大用户和企业已成为网络攻击的主要目标。众所周知&#xff0c;信息安全专业人员都会使用一系列技术工具来协助他们的活动。但是设置环境&#xff0c;安装这些工具…

【牛客 - 188D 】愤怒(01滚动数组优化dp,括号匹配方案个数,tricks)

题干&#xff1a; 小w很生气 小w有一个长为n的括号序列 愤怒小w想把这个括号序列分为两个括号序列 小w想让分为的这两个括号序列同时合法 小w想知道一共有多少种划分方案 (划分的意思是划分为两个子序列) 注意两个序列是 A,B 和 两个序列是B,A 算两种方案,也就是同一位置位…

android adb 开机广播,Android中常用的adb指令

1、安装apkadb install filename.apk如:adb install C:\Users\zhijianhulian\Desktop\keystore\kingoroot.apk2、卸载apkadb uninstall apk包名如&#xff1a;adb uninstall com.kingoroot.cn3、启动activityadb shell am start -n 包名/activity完整包名地址如&#xff1a;adb…