题目链接:C. Soldier and Cards

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.

题意描述:游戏双方各有一些沓牌,牌上都有一个各不相同的值,每一轮取出最上面的牌进行比较,大的一方把对方的牌压在自己的一沓牌的下面,然后再把自己的这张牌再次压在最下面,就这样一直进行下去,直到有一方没有牌或者不会出现没有牌的情况为止。

算法分析:一看这题就知道没有什么算法,在判断游戏会一直进行下去的时候,怎么样来标记之前出现过的一沓牌的顺序,如果之后游戏双方均同时出现之前的一沓牌的顺序的状态,那么这次游戏就会一直进行下去,不会停止,此时想到一沓牌的顺序哈希成为一个值,这样就可以标记一下啦。

可惜昨晚做的时候真心日天了,哈希的方法出现很大的错误,导致电脑没电的时候也没有找到错误,难道我现在真不适合熬夜了吗?fuck,fuck,damn,damn。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int mod=; int n,k1,k2;
int an[],bn[],cn[],dn[];
int vis[][]; int main()
{
while (scanf("%d",&n)!=EOF)
{
int sum=,sum2=;
memset(vis,,sizeof(vis));
scanf("%d",&k1);
for (int i= ;i<=k1 ;i++)
{
scanf("%d",&an[i]);
cn[i]=an[i];
sum += an[i]*an[i]*i;
}
scanf("%d",&k2);
for (int i= ;i<=k2 ;i++)
{
scanf("%d",&bn[i]);
dn[i]=bn[i];
sum2 += bn[i]*bn[i]*i;
}
sum %= mod ;sum2 %= mod ;
vis[sum][sum2]=;
sum=sum2=;
int flag=,ans=;
int i=,j=,c=k1,d=k2;
while (i<=c && j<=d)
{
// cout<<"debug\n";
// for (int u=i ;u<=c ;u++) cout<<cn[u]<<" ";
// cout<<endl;
// for (int u=j ;u<=d ;u++) cout<<dn[u]<<" ";
// cout<<"debug end"<<endl;
if (i!= && j!=)
{
flag=;
sum=;
for (int u=i ;u<=c ;u++) sum += cn[u]*cn[u]*(u-i+);
sum2=;
for (int u=j ;u<=d ;u++) sum2 += dn[u]*dn[u]*(u-j+);
sum %= mod ;sum2 %= mod ;
if (vis[sum][sum2])
{
flag=;
break;
}
vis[sum][sum2]=;
}
if (cn[i]>dn[j])
{
cn[++c]=dn[j] ;cn[++c]=cn[i] ;
i++ ;j++ ;
ans ++ ;
}
else
{
dn[++d]=cn[i] ;dn[++d]=dn[j] ;
i++ ;j++ ;
ans ++ ;
}
}
if (flag) printf("-1\n");
else if (i>c) printf("%d 2\n",ans);
else printf("%d 1\n",ans);
}
return ;
}

cf 546C Soldier and Cards的更多相关文章

  1. 546C. Soldier and Cards

    题目链接 题意 两个人玩扑克,共n张牌,第一个人k1张,第二个人k2张 给定输入的牌的顺序就是出牌的顺序 每次分别比较两个人牌的第一张,牌上面数字大的赢,把这两张牌给赢的人,并且大的牌放在这个人的牌最 ...

  2. CF Soldier and Cards (模拟)

    Soldier and Cards time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

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

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

  4. 【codeforces 546C】Soldier and Cards

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 队列 Soldier and Cards

    Soldier and Cards 题目: Description Two bored soldiers are playing card war. Their card deck consists ...

  6. 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 ...

  7. Codeforces Round #304 (Div. 2) C. Soldier and Cards —— 模拟题,队列

    题目链接:http://codeforces.com/problemset/problem/546/C 题解: 用两个队列模拟过程就可以了. 特殊的地方是:1.如果等大,那么两张牌都丢弃 : 2.如果 ...

  8. C - Soldier and Cards

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Two bo ...

  9. queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

    题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...

随机推荐

  1. obj = object(),所创建的obj实例到底是个啥?

    In[1]: dir(obj) Out[1]:['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '_ ...

  2. SPOJ - DQUERY(区间不同数+树状数组)

    链接:SPOJ - DQUERY 题意:求给定区间不同数的个数(不更新). 题解:离线+树状数组. 对所求的所有区间(l, r)根据r从小到大排序.从1-n依次遍历序列数组,在树状数组中不断更新a[i ...

  3. Windows添加自定义服务、批处理文件开机自启动方法

    [Windows 添加自定义服务方法]: 1.使用Windows服务工具instsrv.exe与srvany.exe: 参考:https://wenku.baidu.com/view/44a6e6f8 ...

  4. Python中关于split和splitext的差别和运用

    在使用Python的过程中,在处理字符串的时候会遇到split()和os.path.split()两个函数,他们的主要区别可以概括为一个从前往后搜索字符串,后者则是从后往前搜索 '.'(reverse ...

  5. PAT 1050 螺旋矩阵

    https://pintia.cn/problem-sets/994805260223102976/problems/994805275146436608 本题要求将给定的 N 个正整数按非递增的顺序 ...

  6. Axure+SVN——实现多人团队开发

    最近进行考试系统重构,一个小组十几个人,这么多人要同时搞需求画原型.这样原本的合作开发工具SVN已经不能满足现在的需求了,这是就找到了一个新的方法--Axure+SVN. 在SVN服务器端建立一个空的 ...

  7. springmvc中RedirectAttributes、SessionFlashMapManager的作用

    RedirectAttributes 在重定向的时候可以传参,不能跨站传参,因为参数是保存在服务器端Session中SessionFlashMapManager 是RedirectAttributes ...

  8. hdu 2510 符号三角形 (DFS+打表)

    符号三角形 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. [洛谷P1792][国家集训队]种树

    题目大意:给出由$n$个数组成的环,取某个数就可以得到它的分数,相邻的两个数不能同时取.问取$m$个数可以得到的最大分数. 题解:建一个大根堆,贪心取,每个点记录前驱后继,取一个点就把前驱后继设成不能 ...

  10. 【转】百度统计js被劫持用来DDOS Github

    原文链接:http://drops.wooyun.org/papers/5398 今天中午刷着全国最大的信息安全从业人员同性交友社区zone.wooyun.org的时候,忽然浏览器每隔2秒就不断的弹窗 ...