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. PowerDesigner反向生成Mysql数据原型

    PowerDesigner反向生成Mysql数据原型 注意事项: (1)JVM 要32位的. (2)需配置JAVA_HOME环境变量指向所需JVM. (3)需配置CLASSPATH环境变量执行 MyS ...

  2. PetaPoco T4模板修改生成实体

    PetaPoco T4 模板生成的实体类全部包含再一个.CS文件中.通过修改PetaPoco的T4模板,生成单文件实体. 1.生成单CS文件模板: SigleFile.ttinclude <#@ ...

  3. ASP.NET读取配置文件发送邮件

    之前写过一篇文章C#使用SMTP发送邮件 后来做了改进,改成读取独立的配置文件,本文只记录读取配置文件的部分,发送部分见上面的链接. 读取配置文件C#代码: using System; using S ...

  4. iisapp 命令 弹出 iisschlp.wsc [88,25] 属性值无效 progid

    iisapp 命令 弹出 iisschlp.wsc [88,25] 属性值无效 progid 在执行iisapp.vbs时,可能会提示如下错误:Windows Script Component - f ...

  5. android 小方法

    小方法 1.获取屏幕分辨率: public class BaseTools { public static int getWindowWidth(Context context) { // 获取屏幕分 ...

  6. MVC的System.Web.Mvc.ViewPage小结

    Inherits="System.Web.Mvc.ViewPage<dynamic>这一句最好是自己手动修改,如果是维护用户数据,用户对象名是User,改成Inherits=&q ...

  7. 关于App.config配置文件

    今天在做复习的时候,突然发现自己无法读取配置文件中的数据库连接字符串,而且检查了半天也没找出原因,最后求助万能的度娘才得以解决—— 1.App.config配置文件在项目中添加后不要修改名称,否则会出 ...

  8. Js弹出层,弹出框代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. PostgreSQL中 AnyElement AnyArray AnynonArray的区别与联系

    http://www.postgresql.org/docs/current/static/extend-type-system.html#EXTEND-TYPES-POLYMORPHIC 先看一个例 ...

  10. CUDA Memories--CUDA记忆体(翻译+整理+测试)

    一边学习一边记录(本文中英结合,专业名词统统不翻译) 在CUDA里,host和devices有不同的记忆体空间. 首先呢,CUDA的memory有很多种类啦 1. Global memory 2. C ...