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. Hello OSGI --- Apache Felix

    Apache Felix Felix是一个OSGi版本4规范的Apache实现. OSGi是一个基于Java的服务平台规范,其目标是被需要长时间运行.动态更新.对运行环境破坏最小化的系统所使用.有许多 ...

  2. Eclipse背景颜色修改

    改变背景颜色(黑底背景的设置) windows->Preferences->General->Editor->Text Editors windows->Preferen ...

  3. IAR Build from the command line 环境变量设置

    http://supp.iar.com/Support/?Note=47884 Technical Note 47884 Build from the command line The alterna ...

  4. 测试HAPROXY的文件分流办法

    测试HAPROXY的文件分流办法 http://blog.chinaunix.net/uid-20553497-id-3054980.html http://blog.sina.com.cn/s/bl ...

  5. 复选框输入Android Studio 如果修改LogCat的颜色,默认全是黑色看着挺不舒服的

    今天一直在查找复选框输入之类的问题,上午正好有机会和大家分享一下. 怎么找到并表现LogCat这里就不需要再讲了吧,主要说一下本篇的主题,如何修改他的颜色 .我们在使用Eclipse的时候应该都用过L ...

  6. Codeforces Gym 100531I Instruction 构造

    Problem I. Instruction 题目连接: http://codeforces.com/gym/100531/attachments Description Ingrid is a he ...

  7. c#winform使用WebBrowser 大全[超长文转载]

    1.主要用途:使用户可以在窗体中导航网页. 2.注意:WebBrowser 控件会占用大量资源.使用完该控件后一定要调用 Dispose 方法,以便确保及时释放所有资源.必须在附加事件的同一线程上调用 ...

  8. [RDLC]报表根据字段列动态加载图片(二)

    参照:http://www.cnblogs.com/hcbin/archive/2010/03/26/1696803.html 改动地方value的值可以用报表的字段进行编辑. 效果:

  9. poj 1941 The Sierpinski Fractal 递归

    //poj 1941 //sep9 #include <iostream> using namespace std; const int maxW=2048; const int maxH ...

  10. Android横竖屏切换及其相应布局载入问题

    第一.横竖屏切换连带载入多屏布局问题: 假设要让软件在横竖屏之间切换.因为横竖屏的高宽会发生转换,有可能会要求不同的布局. 能够通过下面两种方法来切换布局: 1)在res文件夹下建立layout-la ...