Problem B 队列
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
4
2 1 3
2 4 2
6 2
3
1 2
2 1 3
-1
Sample Output
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int main()
{
int n,k1,k2,x;
int flag=0,kase=0, win;
while(scanf("%d",&n)==1&&n)
{
queue<int>q1,q2;
scanf("%d",&k1);
for(int i=0;i<k1;i++)
{
scanf("%d",&x);
q1.push(x);
}
scanf("%d",&k2);
for(int i=0;i<k2;i++)
{
scanf("%d",&x);
q2.push(x);
}
while(n)
{
int y;
x=q1.front();
y=q2.front();
q1.pop();
q2.pop();
kase++;
if(kase==1e3)
break;
if(x>y)
{
q1.push(y);
q1.push(x);
}
else if(x<y)
{
q2.push(x);
q2.push(y);
}
if(q1.size()==0||q2.size()==0)
{
win=q1.empty()?2:1;
flag=1;
break;
} }
if(flag)
printf("%d %d\n",kase,win);
else
printf("-1\n");
}
return 0;
}
Problem B 队列的更多相关文章
- Wannafly 挑战赛 19 参考题解
这一次的 Wannafly 挑战赛题目是我出的,除了第一题,剩余的题目好像对大部分算法竞赛者来说好像都不是特别友好,但是个人感觉题目质量还是过得去的,下面是题目链接以及题解. [题目链接] Wanna ...
- hdu 3706 Second My Problem First 单调队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3706 Second My Problem First Time Limit: 12000/4000 M ...
- HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/500 ...
- HDU 6319.Problem A. Ascending Rating-经典滑窗问题求最大值以及COUNT-单调队列 (2018 Multi-University Training Contest 3 1001)
2018 Multi-University Training Contest 3 6319.Problem A. Ascending Rating 题意就是给你长度为k的数列,如果数列长度k<n ...
- HDU 6319 Problem A. Ascending Rating(单调队列)
要求一个区间内的最大值和每次数过去最大值更新的次数,然后求每次的这个值异或 i 的总和. 这个序列一共有n个数,前k个直接给出来,从k+1到n个数用公式计算出来. 因为要最大值,所以就要用到单调队列, ...
- 循环队列(Joseplus Problem)
#include <iostream> #include <stdio.h> using namespace std; ]; ; void Enqueue(int x) { ) ...
- 2016集训测试赛(二十)Problem A: Y队列
Solution 考虑给定一个\(n\), 如何求\(1\)到\(n\)的正整数中有多少在队列中. 不难注意到我们只需要处理质数次方的情况即可, 因为合数次方会被其因数处理到. 同时我们考虑到可能存在 ...
- Second My Problem First HDU - 3706 单调队列
单调队列 单调队列是指一个队列内部的元素具有严格单调性的一种数据结构,分为单调递增队列和单调递减队列. 单调队列满足两个性质 1.单调队列必须满足从队头到队尾的严格单调性. 2.排在队列前面的比排在队 ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- HTML5地理位置概述和地理位置对象的详解
一.地理位置 经度 : 南北极的连接线 纬度 : 东西连接的线 二.位置信息从何而来 IP地址 GPS全球定位系统 Wi-Fi无线网络 基站 三.地理位置对象(navi ...
- 根据用户输入的二代身份证号码自动为其计算生成出生日期的javascript方法
if(idTypeIsChecked.checked == true){//如果选中的是二代身份证 var birthDateValue = document.getElementById('idNu ...
- XMPP 常见错误:(<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-autho
1检查jid XMPP认证的问题,一般都是与JID有关的,因此解决这类问题,首选的入口就是分析一下XMPPStream的JID,客户端在与服务器连接后,Socket就绑定了这个端口,用来处理与服务器的 ...
- Facade 运行机制
举一个常见的例子在routes.php路由文件中的Route就是使用了laravel的Facade; config/app.php 文件中的aliases数组: 再例如其中的mail: 里面只是简单的 ...
- wordpress内存不足问题“Fatal error:out of memoryin etc...”
2016年3月22日wordpress用户名及密码登录,出现: “ Fatal error: Out of memory (allocated 19136512) (tried to allocate ...
- (01)odoo8.0_Ubuntu14.04源码安装
作者:陈伟明联系 : QQ 942923305 | 微信 toby942923305E-mail: toby2chen@hotmail.com============================ ...
- eclipse hibernate 插件测试1
今天先测试了hibernate tools 安装 在eclipse marketplace里面搜索 hibernate tools 就能找到 网上很多文章所说的使用 install new softw ...
- 串口调试,提示the given port name does not start with COM/com异常解决办法,,发现是打印机在搞怪
串口测试时,用到串口,把打印机的拔下来,换上测试的,程序一打开就提示错误:the given port name does not start with COM/com or does not res ...
- 常用的js函数
function $(){ return document.getElementById(arguments[0])}; /** * 得到上一个元素 * @param {Object} elem */ ...
- 25款漂亮的 WordPress 杂志主题
WordPress是一个免费和开源博客工具和一个内容管理系统(CMS)基于 PHP 和 MySQL,它运行在一个 Web 托管服务.每个月的 WordPress 主题是由开发人员,其中用户喜欢使用 W ...