Soldier and Cards

CodeForces - 546C

Two bored soldiers are playing card war. Their card deck consists of exactly ncards, 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.

Examples

Input
4
2 1 3
2 4 2
Output
6 2
Input
3
1 2
2 1 3
Output
-1

sol: 想了半天也没什么好方法,这个数据范围这么小(n<=10),直接暴力模拟,如果次数超过500就puts("-1")
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
int n;
queue<int>Q1,Q2;
int main()
{
int i,ans=;
R(n);
R(n); while(n--) Q1.push(read());
R(n); while(n--) Q2.push(read());
while((!Q1.empty())&&(!Q2.empty()))
{
int a=Q1.front(),b=Q2.front();
if((++ans)>) break;
if(a>b)
{
Q1.push(b);
Q1.push(a);
}
else
{
Q2.push(a);
Q2.push(b);
}
Q1.pop(); Q2.pop();
}
if(Q1.empty())
{
W(ans); Wl();
}
else if(Q2.empty())
{
W(ans); Wl();
}
else puts("-1");
return ;
}
/*
input
4
2 1 3
2 4 2
output
6 2 input
3
1 2
2 1 3
output
-1
*/
 

codeforces546C的更多相关文章

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

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

随机推荐

  1. MVC架构在Asp.net中的应用和实现

    转载自:http://www.cnblogs.com/baiye7223725/archive/2007/06/07/775390.aspx 摘要:本文主要论述了MVC架构的原理.优缺点以及MVC所能 ...

  2. 在Ubuntu中部署并测试HyperLedger Fabric 0.6

    最近开始研究区块链,对这个新兴的技术有了基本概念上的了解,所以打算基于一个开源项目做做实验.如果是做数字货币,那么比特币的源代码是最好的了,不过这算是区块链1.0吧,已经有很多改进的竞争币和山寨币出来 ...

  3. face recognition[Euclidean-distance-based loss][Center Face]

    本文来自<A Discriminative Feature Learning Approach for Deep Face Recognition>,时间线为2016年.采用的loss是C ...

  4. OPPO Developers Conference(2018.12.26)

    时间:2018.12.26地点:北京国家会议中心

  5. Eclipse中快速打开文件所在的文件夹位置

    本篇文章是紧接着Elicpse使用技巧-打开选中文件文件夹或者包的当前目录文章写的,本文主要是讲的利用eclipse插件的方式打开文件夹的位置, 由于eclipse版本的区别,所以插件也分成两种(实测 ...

  6. 深入浅出Tomcat系列

    原本打算一篇文章就发了的,无奈文章太长,阅读压力较大.为了让阅读体验更好一些,还是分多篇吧,大概6篇. 下面是这个主题的目录: 深入浅出Tomcat/1- 来历和配置文件 深入浅出Tomcat/2 - ...

  7. mysqldump 和mysqlbinlog

    一.mysqldump 1.备份test库 #mysqldump -uroot -p' test >test.sql 2.备份 -B参数 ' -B test >test_B.sql --B ...

  8. jquery判断<inpur type="checkbox" checked>是否被选择

    建议使用 $('#isCheck').attr('checked') 这样的,利于判断 console.log($('#isCheck').prop('checked')); 可以看出prop当che ...

  9. Latex(表格|图片(一丢丢))

    目录 普通的例子 Notation 例子 p{width} 列分割符 @{} \multicolumn supertabular | longtabular 浮动体 table 浮动体 图片 \use ...

  10. SPA单页面优缺点

    优点: 1.体验好,不刷新,减少 请求  数据ajax异步获取 页面流程: 2.前后端分离 3.减轻服务端压力 4.共用一套后端程序代码,设配多端 缺点: 1.首屏加载过慢: 2.SEO 不利于搜索引 ...