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 ...
随机推荐
- linux 输出重定向一份到本地文件,屏幕继续输出
ls -al 2>&1 | tee xLog
- 关于JavaScript中对象的继承实现的学习总结
一.原型链 JavaScript 中原型链是实现继承的主要方法.其主要的思想是利用原型让一个引用类型继承另一个引用类型的属性和方法.实现原型链有一种基本模式,其代码如下. function Super ...
- Java反射-方法(Method)
工作了三年,第二次使用反射! 遇到的问题描述: 多个页面查询后,返回的List中的对象属性为“.00”,页面显示不友好. 查询原因是因为查询数据的SQL为:to_char(a.applyAmount, ...
- Hello 2016
Hello 2016 I am really happy to work and study here. Nothing is better than be oneself ! It's import ...
- 移动前端头部mete
原文链接:http://caibaojian.com/mobile-meta.html//code from http://caibaojian.com/mobile-meta.html<!DO ...
- 正在使用广告标识符 (IDFA)
APP提交审核后,apple方面一直说我使用了IDFA,APP里没有集合任何广告SDK. 怀疑是其他第三方的SDK用了. 检测命令 //在项目的根目录下用终端执行 grep -r advertisin ...
- Java计算程序运行时间
public static void main(String[] args) { // TODO Auto-generated method stub long nd = 1000 * 24 * 60 ...
- Quartz表达式详解(转载)
1. CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 ...
- CentOS用yum安装、配置MariaDB
.创建/etc/yum.repos.d/MariaDB.repo文件,这里用到了刚刚发布正式版的10. [mariadb] name = MariaDB baseurl = http://yum.ma ...
- 一步步教你Hadoop多节点集群安装配置
1.集群部署介绍 1.1 Hadoop简介 Hadoop是Apache软件基金会旗下的一个开源分布式计算平台.以Hadoop分布式文件系统HDFS(Hadoop Distributed Filesys ...