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 ...
随机推荐
- POJ 2983-Is the Information Reliable
Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years a ...
- DataBase -- SQL INNER JOIN
SQL INNER JOIN 关键字 在表中存在至少一个匹配时,INNER JOIN关键字返回行. INNER JIN关键字语法 SELECT column_name(s) FROM table_na ...
- Linux下从零开始部署和使用Jaeger
最近在折腾Jaeger,Jaeger官网都是介绍如何通过Docker部署,二进制部署文档基本没有(已咨询过作者,作者说没文档!你参考Docker自己部署好了!!!),所以打算写一篇Linux部署. J ...
- [zoj] 1081 Points Within || 判断点是否在多边形内
原题 多组数据. n为多边形顶点数,m为要判断的点数 按逆时针序给出多边形的点,判断点是否在多边形内,在的话输出"Within",否则输出"Outside" / ...
- POJ 1064 Cable master | 二分+精度
题目: 给n个长度为l[i](浮点数)的绳子,要分成k份相同长度的 问最多多长 题解: 二分长度,控制循环次数来控制精度,输出也要控制精度<wa了好多次> #include<cstd ...
- TJOI2018游记
D1T1 - 数学计算 直接用线段树/平衡树维护所有数的积即可.我思想僵化写了一个数学方法...应该是能做\(\bmod\)所有数的乘除法. 时间复杂度\(O(nlogn)\). D1T2 - 智力竞 ...
- 疫情控制 blockade
疫情控制 blockad 题目描述 H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树, 1 号城市是首都, 也是树中的根节点. H 国的首都爆发了一种危害性极高的传染病.当 ...
- Java基础语法实例(1)——实习第一天
来到广州实习的第一天,我选择的是JavaEE,因为以后的方向是Java,所以就选择了它.感觉有一段时间没有接触Java了.趁此机会好好努力,将基础巩固好. Java输入及循环,判断,字符转换,数组定义 ...
- 用基于类的通用视图处理表单(Class-based generic views)
处理表单通常包含3步: 初始化GET(空白的后者预填充的表单) POST非法数据(通常重新显示带有错误信息的表单) POST合法数据(提交数据并重定向) 为了将你从这些烦人的重复步骤中解救出来,Dja ...
- KMP字符串匹配算法翔解❤
看了Angel_Kitty学姐的博客,我豁然开朗,写下此文: 那么首先我们知道,kmp算法是一种字符串匹配算法,那么我们来看一个例子. 比方说,现在我有两段像这样子的字符串: 分别是T和P,很明显,P ...