C - Soldier and Cards
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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.
Sample Input
Input4
2 1 3
2 4 2Output6 2Input3
1 2
2 1 3Output-1Hint
First sample:
Second sample:
题意:
两个人打牌,共有n张各不相同的牌,两人分别有k1和k2张,每人从牌堆顶取一张牌对比,牌大的一方分别将对方牌放入牌堆底,再将自己出的牌放入牌堆底。如此进行直到某一方牌堆为空。如果不能结束则输出-1.
典型的队列应用嘛~说来惭愧队列的基本操作都忘得差不多了,做题时还腆着脸地去看了一下以前写的博客。。
按照要求入队出队就可以了,还有需要注意的就是开始输入时的顺序,千万不要先将k1、k2输完再循环输入各自的牌值。
还有一件事,判断死循环的话实在是太麻烦了!!!在此处我取了个巧,将循环次数>100000全部判定为死循环,意料之中的A了 ~ 不要打我【抱头
附AC代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; int main(){
int n,k1,k2,x;
cin>>n>>k1;//注意是k1、k2分开输
queue<int> l;
queue<int> r;
for(int i=;i<k1;i++){//入队
cin>>x;
l.push(x);
}
cin>>k2;
for(int i=;i<k2;i++){
cin>>x;
r.push(x);
}
//cout<<r.front()<<" "<<l.front()<<endl;
int ans=;
while(){
if(l.front()>r.front()){//当1大时按顺序分别将r的队顶元素和l的队顶元素插入l队尾
l.push(r.front());
l.push(l.front());
l.pop();//注意弹出已插入的元素
r.pop();
ans++;
}
else if(l.front()<r.front()){//同上
r.push(l.front());
r.push(r.front());
r.pop();
l.pop();
ans++;
}
if(l.empty()){//若l为空时,2 win
cout<<ans<<" "<<""<<endl;
return ;
}
if(r.empty()){
cout<<ans<<" "<<""<<endl;
return ;
}
if(ans==){//取巧了 哈哈
cout<<"-1"<<endl;
return ;
}
}
return ;
}
C - Soldier and Cards的更多相关文章
- 队列 Soldier and Cards
Soldier and Cards 题目: Description Two bored soldiers are playing card war. Their card deck consists ...
- CF Soldier and Cards (模拟)
Soldier and Cards time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 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 ...
- cf 546C Soldier and Cards
题目链接:C. Soldier and Cards Two bored soldiers are playing card war. Their card deck consists of exact ...
- Codeforces Round #304 (Div. 2) C. Soldier and Cards —— 模拟题,队列
题目链接:http://codeforces.com/problemset/problem/546/C 题解: 用两个队列模拟过程就可以了. 特殊的地方是:1.如果等大,那么两张牌都丢弃 : 2.如果 ...
- 【CodeForces - 546C】Soldier and Cards (vector或队列)
Soldier and Cards 老样子,直接上国语吧 Descriptions: 两个人打牌,从自己的手牌中抽出最上面的一张比较大小,大的一方可以拿对方的手牌以及自己打掉的手牌重新作为自己的牌, ...
- queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards
题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...
- 【codeforces 546C】Soldier and Cards
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 546C. Soldier and Cards
题目链接 题意 两个人玩扑克,共n张牌,第一个人k1张,第二个人k2张 给定输入的牌的顺序就是出牌的顺序 每次分别比较两个人牌的第一张,牌上面数字大的赢,把这两张牌给赢的人,并且大的牌放在这个人的牌最 ...
随机推荐
- [vxlan] 二 什么是VXLAN
VXLAN是一种mac in UDP的技术.简单讲就是传统的二层帧被封装到了UDP的package中.通过UDP的IP网络发送到目的地然后再解封装. VXLAN 跟VLAN对比,最重要的一个概念就是V ...
- Docker 的CMD与ENTRYPOINT区别
我们在构建一个docker镜像的时候,Dockerfile里面有两个命令会引起我们的注意,它们就是 CMD 和 ENTRYPOINT,看起来很相似,实际上并非如此. 一.CMD 顾名思义就是允许用户指 ...
- BroadcastReceiver详解(一)
今天我们来讲一下Android中BroadcastReceiver的相关知识. BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在And ...
- Android的activity的生命周期
activity的生命周期有四个重要的状态:活动,暂停,停止,销毁 基本的方法有:onCreate.onStart.onResume.onPause.onStop.onDestory 最经常使用的方法 ...
- 关于 thinkPHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback 关于thinkPHP rpc调 ...
- Android平台Camera实时滤镜实现方法探讨(十一)--实时美颜滤镜
上一章完毕了对图片的磨皮处理.经过简单算法流程优化,能够达到非常快的速度.可是不能用于实时美颜.经实验,若採用仅仅处理Y信号的方案.半径极限大约是5-10,超过10则明显感受到卡顿.但对于1920X1 ...
- Jenkins系列之-—07 集成JIRA
一.Jenkins Jira插件安装&配置 1. 安装插件,主要安装如下插件: Jira Issue Updater 该插件用于更新JIRA ISSUES 的工作流状态或增加备注 JIRA p ...
- uva 11468 - Substring(AC自己主动机+概率)
题目链接:uva 11468 - Substring 题目大意:给出一些字符和各自字符相应的选择概率.随机选择L次后得到一个长度为L的字符串,要求该字符串不包括随意一个子串的概率. 解题思路:构造AC ...
- Redis实现消息的发布/订阅
利用spring-boot结合redis进行消息的发布与订阅: 发布: class Publish { private static String topicName = “Topic:chat”; ...
- BZOJ 3992: [SDOI2015]序列统计 快速幂+NTT(离散对数下)
3992: [SDOI2015]序列统计 Description 小C有一个集合S,里面的元素都是小于M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数列中的每个数都属于集合S ...