Soldier and Cards
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
input
  1. 4
    2 1 3
    2 4 2
output
  1. 6 2
input
  1. 3
    1 2
    2 1 3
output
  1. -1
  2.  
  3. 纯链表模拟,怎么判断无解当时没想到,所以开了个很大的循环,如果这个循环内还没算出的话就无解。
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdio>
  4. #include <string>
  5. #include <queue>
  6. #include <vector>
  7. #include <map>
  8. #include <algorithm>
  9. #include <cstring>
  10. #include <cctype>
  11. #include <cstdlib>
  12. #include <cmath>
  13. #include <ctime>
  14. using namespace std;
  15.  
  16. struct Node
  17. {
  18. int num;
  19. Node * next;
  20. };
  21.  
  22. int main(void)
  23. {
  24. Node * first_1,* first_2,* last_1,* last_2;
  25. Node * cur,* front;
  26. int n,k_1,k_2,num,count = ;
  27.  
  28. scanf("%d",&n);
  29.  
  30. scanf("%d",&k_1);
  31. for(int i = ;i < k_1;i ++)
  32. {
  33. scanf("%d",&num);
  34. cur = new Node;
  35. cur -> num = num;
  36. cur -> next = nullptr;
  37. if(!i)
  38. {
  39. front = first_1 = cur;
  40. last_1 = cur;
  41. continue;
  42. }
  43. front -> next = cur;
  44. front = cur;
  45. last_1 = cur;
  46. }
  47.  
  48. scanf("%d",&k_2);
  49. for(int i = ;i < k_2;i ++)
  50. {
  51. scanf("%d",&num);
  52. cur = new Node;
  53. cur -> num = num;
  54. cur -> next = nullptr;
  55. if(!i)
  56. {
  57. front = first_2 = cur;
  58. last_2 = cur;
  59. continue;
  60. }
  61. front -> next = cur;
  62. front = cur;
  63. last_2 = cur;
  64. }
  65.  
  66. while(first_1 && first_2)
  67. {
  68. Node * temp_1 = first_1;
  69. Node * temp_2 = first_2;
  70.  
  71. if(temp_1 -> num < temp_2 -> num)
  72. {
  73. Node * cur_1 = new Node;
  74. Node * cur_2 = new Node;
  75. cur_1 -> num = temp_1 -> num;
  76. cur_2 -> num = temp_2 -> num;
  77.  
  78. last_2 -> next = cur_1;
  79. cur_1 -> next = cur_2;
  80. cur_2 -> next = nullptr;
  81. last_2 = cur_2;
  82. }
  83. else
  84. {
  85. Node * cur_1 = new Node;
  86. Node * cur_2 = new Node;
  87. cur_1 -> num = temp_1 -> num;
  88. cur_2 -> num = temp_2 -> num;
  89.  
  90. last_1 -> next = cur_2;
  91. cur_2 -> next = cur_1;
  92. cur_1 -> next = nullptr;
  93. last_1 = cur_1;
  94. }
  95.  
  96. first_1 = first_1 -> next;
  97. first_2 = first_2 -> next;
  98. count ++;
  99. if(count > )
  100. {
  101. puts("-1");
  102. return ;
  103. }
  104. }
  105. if(!first_1)
  106. printf("%d 2\n",count);
  107. else
  108. printf("%d 1\n",count);
  109.  
  110. return ;
  111. }

CF Soldier and Cards (模拟)的更多相关文章

  1. Codeforces Round #304 (Div. 2) C. Soldier and Cards —— 模拟题,队列

    题目链接:http://codeforces.com/problemset/problem/546/C 题解: 用两个队列模拟过程就可以了. 特殊的地方是:1.如果等大,那么两张牌都丢弃 : 2.如果 ...

  2. [CF546C] Soldier and Cards - 模拟

    两个人玩牌,首先两个人都拿出自己手牌的最上面的进行拼点,两张拼点牌将都给拼点赢得人,这两张牌放入手牌的顺序是:先放对方的牌再放自己的.若最后有一个人没有手牌了,那么他就输了,求输出拼点的次数和赢得人的 ...

  3. queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

    题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...

  4. cf 546C Soldier and Cards

    题目链接:C. Soldier and Cards Two bored soldiers are playing card war. Their card deck consists of exact ...

  5. 【CodeForces - 546C】Soldier and Cards (vector或队列)

    Soldier and Cards 老样子,直接上国语吧  Descriptions: 两个人打牌,从自己的手牌中抽出最上面的一张比较大小,大的一方可以拿对方的手牌以及自己打掉的手牌重新作为自己的牌, ...

  6. 【codeforces 546C】Soldier and Cards

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 队列 Soldier and Cards

    Soldier and Cards 题目: Description Two bored soldiers are playing card war. Their card deck consists ...

  8. Codeforces Round #304 (Div. 2) C. Soldier and Cards 水题

    C. Soldier and Cards Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/546 ...

  9. C - Soldier and Cards

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Two bo ...

随机推荐

  1. css样式被覆盖解决方案

    刚才写zenktodo的时候,通过动态添加class的方式修改一个div的样式,总是不起作用. #navigator { height: 100%; width: 200; position: abs ...

  2. 操作无法完成,因为文件夹已在另一个程序中打开(the action can't be completed because the folder or a file in it is open in another program)

    解决方法: 启动任务管理器——性能——资源监视器——CPU选项卡——关联的句柄——搜索句柄 ——(输入)要删除的文件夹名——搜索到与文件夹名句柄相关联的进程 (由于此程序进程正在调用文件夹,才造成了对 ...

  3. MON166 FAQ

    MON166: SOFTWARE RESET USING THE MONITOR QUESTION What happens when debugging using MON166 and my pr ...

  4. uva331 - Mapping the Swaps

    Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...

  5. DAG成员服务器还原

    DAG成员服务器 exmb02 已损坏: 1.使用 Get-MailboxDatabase cmdlet 为要恢复的服务器上的任何邮箱数据库副本检索所有重播延迟和截断延迟设置:   Get-Mailb ...

  6. Drupal 7.31 SQL注入漏洞利用具体解释及EXP

     有意迟几天放出来这篇文章以及程序,只是看样子Drupal的这个洞没有引起多少重视,所以我也没有必要按着不发了,只是说实话这个洞威力挺大的.当然.这也是Drupal本身没有意料到的. 0x00 首 ...

  7. JavaScript的角色巨变和Web技术的发展

    曾经JavaScript是职业程序员看不上眼的脚本语言,如今只有高级程序员才能驾驭它. JavaScript性质和地位的天翻地覆,正是Web技术飞速变化的印证. 最初职业程序员轻视JavaScript ...

  8. hdu 4740 The Donkey of Gui Zhou bfs

    The Donkey of Gui Zhou Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproble ...

  9. 战舰少女 黑暗炼钢 按键精灵 代码及apk下载

    注: 该代码仅仅适用于1920*1080分辨率的android手机,因为我只有这个分辨率的手机TnT 代码其实蛮简单的,都是比较简单的模拟就好了…… 要改也比较轻松吧 APK下载地址:链接: http ...

  10. [每日一题] OCP1z0-047 :2013-08-26 TIMESTAMP WITH LOCAL TIME ZONE....................112

    正确答案:C 使用TIMESTAMP WITH LOCAL TIME ZONE数据类型,用户插入数据时,oracle将会把用户的数据结合用户session的时区信息自动换算成数据库设定的时区的时间进行 ...