CF 546C Soldier and Cards
2015-10-26 22:23
232 查看
C. Soldier and Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 test(s)
input
output
input
output
Note
题目意思是两个人玩牌,首先两个人都拿出自己手牌的最上面的进行拼点,两张拼点牌将都给拼点赢得人,这两张牌放入手牌的顺序是:先放对方的牌再放自己的。若最后有一个人没有手牌了,那么他就输了,求输出拼点的次数和赢得人的编号,如果一直无法结束比赛,则输出-1.
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 test(s)
input
4 2 1 3 2 4 2
output
6 2
input
3 1 2 2 1 3
output
-1
Note
题目意思是两个人玩牌,首先两个人都拿出自己手牌的最上面的进行拼点,两张拼点牌将都给拼点赢得人,这两张牌放入手牌的顺序是:先放对方的牌再放自己的。若最后有一个人没有手牌了,那么他就输了,求输出拼点的次数和赢得人的编号,如果一直无法结束比赛,则输出-1.
#include<iostream> #include<queue> using namespace std; queue<int>q1; queue<int>q2; int main() { int n; cin >> n; int n1; cin >> n1; int a; for(int i = 0;i<n1;i++) { cin >> a; q1.push(a); } int n2; cin >> n2; for(int i = 0;i<n2;i++) { cin >> a; q2.push(a); } int ans = 0; while(ans<40000&&!q1.empty()&&!q2.empty()) { int v1 = q1.front(); q1.pop(); int v2 = q2.front(); q2.pop(); if(v1>v2) { q1.push(v2); q1.push(v1); } else { q2.push(v1); q2.push(v2); } ans++; } if(ans==40000)cout<<-1<<endl; else { if(q1.empty())cout<<ans<<" "<<2<<endl; else cout<<ans<<" "<<1<<endl; } return 0; }
相关文章推荐
- 黑马程序员——Java 基础:权限
- 让人无比郁闷的No result defined for action 错误
- oracle表的操作sql语句
- iOS 3d touch设计的秘密
- HDU - 1711 Number Sequence(KMP)
- Web应用中request获取path,URI,URL
- filddler学习
- 1.程序,进程,文档,内存,外存-讲义大纲
- C++primer学习:重载运算符(3)
- 6174的问题
- 欢迎使用CSDN-markdown编辑器
- ARM9开发板FL2440移植Linux-3.0内核————触摸屏的移植
- Java语言实现的单例设计模式复习
- id ,NSObject, id<NSObject>区别
- CHUTTERAN对界面切图的好处和教程
- 关于UI设计师面试的那些事儿
- 我为什么要写blog
- Longest Common Prefix
- 关于public定义方法和类的作用,以及包和import语句
- Java中抽象类和接口的区别