CodeForces-546C-Soldier and Cards
2016-04-14 09:37
218 查看
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 thisfight 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 followk1 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 offights before end of game and the second one is1 or 2 showing which player has won.
If the game won’t end and will continue forever output - 1.
Sample Input
Input
4
2 1 3
2 4 2
Output
6 2
Input
3
1 2
2 1 3
Output
-1
Hint
First sample:
Second sample:
有两个人,每个人又一些牌,总张数不超过10,每张牌都有个不同价值,每个人的牌的标号固定的,出牌只能选标号最小的出,然后比较大小,大的那个获胜,得到对方的牌,并把获得的这张牌与自己出的那张牌,都放到尾部,然后如此循环,最后没有手牌的人输掉比赛
我一看拿前面的,放到后面就直接想到用队列模拟了,然后随便设置了一个次数,没想到对了,但是队列清空那个地方我卡了很久
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 thisfight 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 followk1 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 offights before end of game and the second one is1 or 2 showing which player has won.
If the game won’t end and will continue forever output - 1.
Sample Input
Input
4
2 1 3
2 4 2
Output
6 2
Input
3
1 2
2 1 3
Output
-1
Hint
First sample:
Second sample:
有两个人,每个人又一些牌,总张数不超过10,每张牌都有个不同价值,每个人的牌的标号固定的,出牌只能选标号最小的出,然后比较大小,大的那个获胜,得到对方的牌,并把获得的这张牌与自己出的那张牌,都放到尾部,然后如此循环,最后没有手牌的人输掉比赛
我一看拿前面的,放到后面就直接想到用队列模拟了,然后随便设置了一个次数,没想到对了,但是队列清空那个地方我卡了很久
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> using namespace std; const int N=1000000; queue <int>va,vb; int main() { int t,a,b,n,m; while(scanf("%d",&t)!=EOF) { while(va.size())va.pop(); while(vb.size())vb.pop(); scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%d",&a); va.push(a); } scanf("%d",&m); for(int i=0; i<m; i++) { scanf("%d",&b); vb.push(b); } int s=0; while(!va.empty()&&!vb.empty()) { s++; if(s>N) break; int vah=va.front(); int vbh=vb.front(); va.pop(); vb.pop(); if(vah<vbh) { vb.push(vah); vb.push(vbh); } if(vah>vbh) { va.push(vbh); va.push(vah); } } if(va.empty()||vb.empty()) { if(va.empty()) { printf("%d 2\n",s); } else printf("%d 1\n",s); } else puts("-1"); } return 0; }
相关文章推荐
- 五子棋算法设计
- 【bzoj2242】[SDOI2011]计算器 数论相关(快速幂+扩展欧几里得+BSGS)
- C语言strstr()函数:返回字符串中首次出现子串的地址
- SQL Server 2008 R2 下移动数据库的存储位置
- CAS4.0 4.1 服务器端搭建(一)
- Jquery实现可拖拽的树菜单
- layoutSubviews
- Spring框架整合Struts2使用Validation框架验证表单用户输入数据的详细教程
- AsyncTask(异步线程)详解
- JProfiler监控本地服务程序性能
- python 小模块--platform
- 63. Unique Paths II【M】【30】
- Web API接口之FileReader
- linux第四章笔记
- 搜索旋转排序数组 II
- iOS instruments 介绍
- bzoj 4454: C Language Practice O(n)-O(1) gcd
- K-means聚类
- Thread中的实例方法
- 基于Java NIO的Socket通信