Codeforces Round #304 C(Div. 2)(模拟)
http://codeforces.com/problemset/problem/546/C
题意:
总共有n张牌,1手中有k1张分别为:x1, x2, x3, ..xk1,2手中有k2张,分别为:y1, y2, ...yk2;(n<=10&&k1+k2==n,所有牌的数字都不同);
依次比较x1, y1的大小,若x1>y1,依次将x1, y1加入x牌最底下;反之则将y1,x1依次加入y牌最底下;直至有方的牌输完为止;输出总共游戏的步数和赢方;
如果两方都不能赢,则输出-1;
思路:直接用栈模拟,关键的地方是判断两方都不能赢的情况,判断方法有两种:
其一是设一个足够大的数,超过这个步数还没有分出输赢情况的话则可以认定
两方都不能赢,因为n<=10,如果能分出输赢的话则500步以内一定会出结果的!
另一种方法是判断当前状态之前是否出现过,若出现过,则其一定不能分出输赢!会死循环!
至于如何判断是否出现过,可以将每个状态都存入一个string数组中,再将当当前状态与之对比,若出现过,则平局;
代码分别如下:
方法1:
#include<bits/stdc++.h>
#define MAXN 10000
#define MAX 1000000000
#define eps 1e-6
#define ll long long
using namespace std; int main(void)
{
queue<int> stk1, stk2;
string str1[MAXN], str2[MAXN];
int n, a, b, ans=;
cin >> n;
cin >> a;
for(int i=; i<a; i++)
{
int x;
cin >> x;
stk1.push(x);
}
cin >> b;
for(int i=; i<b; i++)
{
int x;
cin >> x;
stk2.push(x);
}
int cnt=, flag;
while()
{
int x=stk1.front(), y=stk2.front();
if(x>y) //****如果x>y,将y, x依次从队尾加入stk1队列,并将stk2,stk1队首元素y, x抛掉;
{
stk1.push(y);
stk1.push(x);
stk1.pop();
stk2.pop();
}
else //****如果x<y,将x, y依次从队尾加入stk2队列,并将stk2, stk1队首元素y, x抛掉;
{
stk2.push(x);
stk2.push(y);
stk2.pop();
stk1.pop();
}
cnt++;
if(stk1.empty()) //****队列stk1为空,即2赢了
{
cout << cnt << " " << "" << endl;
return ;
}
if(stk2.empty()) //****队列stk2为空,即1赢了
{
cout << cnt << " " << "" << endl;
return ;
}
if(cnt>) break; //****如果步数大于500还没分输赢,即可判平局
}
cout << "-1" << endl;
return ;
}
方法2:
#include<bits/stdc++.h>
#define MAXN 10000
#define MAX 1000000000
#define eps 1e-6
#define ll long long
using namespace std; int main(void)
{
queue<int> stk1, stk2;
string str1[MAXN], str2[MAXN];
int n, a, b, ans=;
cin >> n;
cin >> a;
for(int i=; i<a; i++)
{
int x;
cin >> x;
stk1.push(x);
}
cin >> b;
for(int i=; i<b; i++)
{
int x;
cin >> x;
stk2.push(x);
}
int cnt=, flag;
while()
{
int low1[MAXN], low2[MAXN], k1=, k2=;
while(!stk1.empty()) //**将stk1存入string数组中
{
low1[k1++]=stk1.front();
stk1.pop();
}
for(int i=; i<k1; i++)
{
stk1.push(low1[i]);
}
for(int i=; i<k1; i++)
{
str1[cnt]+=to_string(low1[i]);
}
while(!stk2.empty()) //****将stk2存入string数组中
{
low2[k2++]=stk2.front();
stk2.pop();
}
for(int i=; i<k2; i++)
{
stk2.push(low2[i]);
}
for(int i=; i<k2; i++)
{
str2[cnt]+=to_string(low2[i]);
}
for(int i=; i<cnt; i++) //***判断当前状态是否出现过,若出现过,则平局
{
if(str1[i]==str1[cnt]&&str2[i]==str2[cnt])
{
cout << "-1" << endl;
return ;
}
}
int x=stk1.front(), y=stk2.front();
if(x>y) //****如果x>y,将y, x依次从队尾加入stk1队列,并将stk2,stk1队首元素y, x抛掉;
{
stk1.push(y);
stk1.push(x);
stk1.pop();
stk2.pop();
}
else //****如果x<y,将x, y依次从队尾加入stk2队列,并将stk2, stk1队首元素y, x抛掉;
{
stk2.push(x);
stk2.push(y);
stk2.pop();
stk1.pop();
}
cnt++;
if(stk1.empty()) //****队列stk1为空,即2赢了
{
flag=;
break;
}
if(stk2.empty()) //****队列stk2为空,即1赢了
{
flag=;
break;
}
}
cout << cnt << " " << flag << endl;
return ;
}
Codeforces Round #304 C(Div. 2)(模拟)的更多相关文章
- queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards
题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas
题目传送门 /* 水题:ans = (1+2+3+...+n) * k - n,开long long */ #include <cstdio> #include <algorithm ...
- 数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!
Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...
- Codeforces 1023 A.Single Wildcard Pattern Matching-匹配字符 (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)
Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Patter ...
- Codeforces Round #581(Div. 2)
Codeforces Round #581(Div. 2) CF 1204 A. BowWow and the Timetable 题解:发现,$4$的幂次的二进制就是一个$1$后面跟偶数个$0$. ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A B C D 水 模拟 二分 贪心
A. Is it rated? time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
随机推荐
- Mongodb——GridFS
GridFS用于存储和恢复那些超过16M(BSON文件限制)的文件. GridFS将文件分成大块,将每个大块存储为单独的文件.GridFS中限制chunk最大为256k.GridFS使用两个colle ...
- ckeditor、ckeditor配置--整合
1.将ckeditor和ckfinder文件夹拷入项目文件夹中,刷新项目. 2.ckfinder把文件夹中的bin目录下的dll文件(CKFinder.dll)添加到网站的引用中,防止出现找不到类的错 ...
- js搜索框输入提示(高效-ys8)
<style type="text/css"> .inputbox .seleDiv { border: 1px solid #CCCCCC; display: non ...
- JQuery data方法的使用-遁地龙卷风
(-1)说明 我用的是chrome49,这个方法涉及到JQuery版本问题,我手里有3.0的,有1.9.1,后面将1.9.1及其以前的称为低版本,3.0称为高版本 测试例子用到的showMessage ...
- python 输入和输出
到目前为止我们遇到过两种输出值的方法: 表达式语句和print语句. (第三个方式是使用文件对象的write()方法: 标准输出文件可以引用 sys.stdout.详细内容参见库参考手册. Pytho ...
- [codevs1141]数列
[codevs1141]数列 试题描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12 ...
- vSphere、Hyper-V与XenServer 你选哪个?
vSphere.Hyper-V与XenServer 你选哪个? 当在VMware vSphere.Microsoft Hyper-V与Citrix Systems XenServer之间做出选择时,v ...
- 阻止a标签默认跳转事件
1:<a href="####"></a> 2:<a href="javascript:void(0)"></a> ...
- poj3984
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- 9.4---集合子集(CC150)
这题非常复杂.牛客网上对应的题目对结果要求比较苛刻,所以要调整. 整体思路是:先放进去一个,然后第二个来的时候插入到已有的,并且把自己也放进去. public static ArrayList< ...