cf 546C Soldier and Cards
题目链接: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.
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.
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的更多相关文章
- 546C. Soldier and Cards
题目链接 题意 两个人玩扑克,共n张牌,第一个人k1张,第二个人k2张 给定输入的牌的顺序就是出牌的顺序 每次分别比较两个人牌的第一张,牌上面数字大的赢,把这两张牌给赢的人,并且大的牌放在这个人的牌最 ...
- CF Soldier and Cards (模拟)
Soldier and Cards time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 【CodeForces - 546C】Soldier and Cards (vector或队列)
Soldier and Cards 老样子,直接上国语吧 Descriptions: 两个人打牌,从自己的手牌中抽出最上面的一张比较大小,大的一方可以拿对方的手牌以及自己打掉的手牌重新作为自己的牌, ...
- 【codeforces 546C】Soldier and Cards
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 队列 Soldier and Cards
Soldier and Cards 题目: Description Two bored soldiers are playing card war. Their card deck consists ...
- 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 ...
- Codeforces Round #304 (Div. 2) C. Soldier and Cards —— 模拟题,队列
题目链接:http://codeforces.com/problemset/problem/546/C 题解: 用两个队列模拟过程就可以了. 特殊的地方是:1.如果等大,那么两张牌都丢弃 : 2.如果 ...
- C - Soldier and Cards
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Two bo ...
- queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards
题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...
随机推荐
- eclipse importing maven projects 卡顿
导入一个maven工程后 一直显示 importing maven projects 9% 解决办法: 找到eclipse安装目录下的eclipse.ini 在最后加入 -vm $JAVA_HOME% ...
- 机器学习框架Tensorflow数字识别MNIST
SoftMax回归 http://ufldl.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92 我们的训练集由 个已标记的样本构成: ,其 ...
- winform中key读取修改
根据key name的名称读取value-----读取使用ConfigurationManager.AppSettings读取容易没读取到根目录中的key public string GetXml(s ...
- hibernate中类状态转换
- Educational Codeforces Round 42 (Rated for Div. 2) A
A. Equator time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- 浅谈Visitor Pattern
第一步: 在介绍Visitor Pattern (访问者模式)之前,先简要介绍一下:双重分派. 在Visitor Pattern中双重分派是指:数据结构的每一个节点都可以接受一个访问者的调用(这句 ...
- Windows彻底删除不用的dc
如果DC迁移,或者多台DC中的某台DC损坏,要退出历史舞台,一定要彻底的卸载,否则,系统会默认存在,一直同步,会出很多问题.怎么做才能彻底的从域中卸载呢?下面介绍一个个人认为很好用的方法:1.在存活的 ...
- Windows转移FSMO角色
转移 FSMO 角色若要使用 Ntdsutil 实用工具转移 FSMO 角色,请按照下列步骤操作:1.登录到基于 Windows 2000 Server 或基于 Windows Server 2003 ...
- ubuntu上安装ffmpeg
安装包和主要步骤: 1. 首先安装系统基础环境 RHEL & CentOS 系列:yum install -y automake autoconf libtool gcc gcc-c++ ...
- .com和.cn域名的区别所在,各个域名后缀含义
很多人在注册域名的时候不明白域名后缀的含义,在这里就介绍两种最为常用的域名,介绍下他们的区别以及适用的范围.需要先查询是否被注册,我们经常去的就是西部数据和万网,查询并注册未被注册的域名,一般无论是什 ...