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

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. java使用org.apache.poi读取与保存EXCEL文件

    一.读EXCEL文件 package com.ruijie.wis.cloud.utils; import java.io.FileInputStream; import java.io.FileNo ...

  2. C:指针、数据类型、格式化输入输出、输入函数的坑点

    指针.数据类型.格式化输入输出.输入函数的坑点 有时候我们迷茫的时候,坚持就是最好的选择. 1.指针的分类为什么很重要? 参考 答:因为指针会根据相应的类型取对应长度的数据,类型决定所取数据的长度.如 ...

  3. Apache开启伪静态后报500错误.

    参考:http://blog.163.com/lgh_2002/blog/static/44017526201051452939761/ 加载Rewrite模块: 在conf目录下httpd.conf ...

  4. InteractivePNG

    在as3中很多时候需要只能选中png中可视区域,即透明区域“感觉可以穿透”.

  5. 利用FlashPaper实现类似百度文库功能

    最近需要实现一个类似百度文库的功能,在Google上淘了一段时间,发现FlashPaper还算能够不错的实现此需求. 首先讲下思路: 1>安装FlashPaper: 2>利用java代码将 ...

  6. CMSIS Example - osTimer osTimerCreate osTimerStart

    osTimerId timer; uint32_t cnt=; void timerHandler( void * arg ) { cnt++; osTimerStart( timer, ); } o ...

  7. MVVM知识库总结

    1.关于MVVM概念的理解和实际运用中的解析: http://www.cnblogs.com/hielvis/archive/2011/03/22/1991959.html http://msdn.m ...

  8. hadoop备战:hadoop,hbase兼容版本号汇总

    Hbase的安装须要考虑Hadoop的版本号,即兼容性.有不足的希望能指出. 下面考究官网得到的,关于hadoop版本号和hbase版本号可到下面网址中下载:http://mirror.bit.edu ...

  9. 可拖拽GridView代码解析

    本片学习笔记是对eoe网上一个项目代码的解读.详细项目作者的博客例如以下:http://blog.csdn.net/vipzjyno1/article/details/26514543.项目源代码下载 ...

  10. Swift学习笔记八

    函数 Swift的函数语法非常独特,也提供了很高的灵活性和可读性.它可以充分表达从简单的无参数C风格函数到复杂的拥有局部变量和外部变量的OC风格的方法.参数可以有默认值,方便函数的调用.Swift中的 ...