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
4
2 1 3
2 4 2
output
6 2
input
3
1 2
2 1 3
output
-1

纯链表模拟,怎么判断无解当时没想到,所以开了个很大的循环,如果这个循环内还没算出的话就无解。
 #include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; struct Node
{
int num;
Node * next;
}; int main(void)
{
Node * first_1,* first_2,* last_1,* last_2;
Node * cur,* front;
int n,k_1,k_2,num,count = ; scanf("%d",&n); scanf("%d",&k_1);
for(int i = ;i < k_1;i ++)
{
scanf("%d",&num);
cur = new Node;
cur -> num = num;
cur -> next = nullptr;
if(!i)
{
front = first_1 = cur;
last_1 = cur;
continue;
}
front -> next = cur;
front = cur;
last_1 = cur;
} scanf("%d",&k_2);
for(int i = ;i < k_2;i ++)
{
scanf("%d",&num);
cur = new Node;
cur -> num = num;
cur -> next = nullptr;
if(!i)
{
front = first_2 = cur;
last_2 = cur;
continue;
}
front -> next = cur;
front = cur;
last_2 = cur;
} while(first_1 && first_2)
{
Node * temp_1 = first_1;
Node * temp_2 = first_2; if(temp_1 -> num < temp_2 -> num)
{
Node * cur_1 = new Node;
Node * cur_2 = new Node;
cur_1 -> num = temp_1 -> num;
cur_2 -> num = temp_2 -> num; last_2 -> next = cur_1;
cur_1 -> next = cur_2;
cur_2 -> next = nullptr;
last_2 = cur_2;
}
else
{
Node * cur_1 = new Node;
Node * cur_2 = new Node;
cur_1 -> num = temp_1 -> num;
cur_2 -> num = temp_2 -> num; last_1 -> next = cur_2;
cur_2 -> next = cur_1;
cur_1 -> next = nullptr;
last_1 = cur_1;
} first_1 = first_1 -> next;
first_2 = first_2 -> next;
count ++;
if(count > )
{
puts("-1");
return ;
}
}
if(!first_1)
printf("%d 2\n",count);
else
printf("%d 1\n",count); return ;
}

最新文章

  1. C# 窗体缩放的时候同步改变控件的大小和字体
  2. PLSQL Developer注册码
  3. python 学习中遇到的问题
  4. 化工厂装箱员(洛谷 P2530)
  5. Foundation和UIKit框架图
  6. MVC小系列(九)【引入namespace】
  7. 【转】Win7+Ubuntu12.04.1硬盘安装错误及解决方案----不错
  8. POJ 2255 Tree Recovery 二叉树恢复
  9. 百度api集合!
  10. 同一台机器上多个tomcat启动造成的内存溢出问题的解决方法。
  11. python/MySQL(索引、执行计划、BDA、分页)
  12. kamailio 云部署 配置NAT
  13. 通过脚本下载GO被墙或常用的相关包
  14. Work Queues
  15. P2089 烤鸡(搜索简单题)
  16. MySQL 的两个特殊属性 unsigned与 zerofill
  17. GuavaCache学习笔记三:底层源码阅读
  18. linux下寻找段错误的方法
  19. 一千行 MySQL 学习笔记
  20. Docker 容器十诫

热门文章

  1. JS、jqueryie6浏览器下使用js无法提交表单的解决办法
  2. Java获取IP地址:request.getRemoteAddr()注意
  3. SQL SERVER 数据库表同步复制 笔记
  4. GET与POST提交
  5. How to Send Information (String, Image, Record) Between Two Applications
  6. ZooKeeper是什么?
  7. iOS CocoaPods安装和使用图解
  8. 使用ActivityGroup来切换Activity和Layout
  9. C++基础之预处理命令
  10. Golang学习 - bytes 包