题目链接: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. (原)UE4 制作执行队列(Action Queue)

    队列和树在游戏开发中是比较常见的数据结构,在一定范围能保证执行的顺序. 结合一些设计模式技巧,往往可以做一些神器.     如加载块chunk管理,任务系统(当然也可以使用行为树来做复杂的任务系统). ...

  2. Leetcode 659.分割数组为连续子序列

    分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [1,2,3 ...

  3. [部署开发环境][1 vagrant] vagrant部署开发环境--安装vagrant

    # 安装教程 # 安装vagrant 教程 # 准备 - windows操作系统 - VirtualBox---Win, - vagrant_1.9.3.msi - 镜像文件https://atlas ...

  4. iOS CGContextRef 画一条直线,仅仅是画一条直线

    今天周末休息,想好好补补课,无奈,弄了一上午,全部都是半边拉块的demo,有一种深深的挫败感. 中午睡醒一觉后,又看了一集“奔跑吧兄弟”,然后一下午时间就过去了. 仔细一想,应该是我的补课方法不对:要 ...

  5. android ViewGroup getChildDrawingOrder与 isChildrenDrawingOrderEnabled()

    getChildDrawingOrder与 isChildrenDrawingOrderEnabled()是属于ViewGroup的方法.   getChildDrawingOrder 用于 返回当前 ...

  6. VB.NET——报表

    在工具箱查找ReportViewer,添加. 选择设计新报表: 排列字段,布局的步骤省略. 完成. 接下来,我们可以更改中文标题,设置背景色等,让界面看起来更美观. 如果需要添加参数,所传递的参数要与 ...

  7. nagios客户端安装

    在被监控服务器(Linux/unix)上安装Nagios-plugins和nrpe 1.添加用户   1 2 ; html-script: false ]/usr/sbin/useradd -m na ...

  8. 第十二篇:HTML基础

    本篇内容 HTML概述 HTML常用基本标签 CSS格式引入 一. HTML概述 1.定义: HTML,超文本标记语言,写给浏览器的语言,目前网络上应用最广泛的语言.HTML也在不断的更新,最新版本已 ...

  9. 【bzoj4636】蒟蒻的数列 离散化+线段树

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...

  10. 洛谷树剖模板题 P3384 | 树链剖分

    原题链接 对于以u为根的子树,后代节点的dfn显然比他的dfn大,我们可以记录一下回溯到u的dfn,显然这两个dfn构成了一个连续区间,代表u及u的子树 剩下的就和树剖一样了 #include< ...