CodeForces - 546C Soldier and Cards(vector存储)
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.
Examples
input
Copy
4 2 1 3 2 4 2
output
Copy
6 2
input
Copy
3 1 2 2 1 3
output
Copy
-1
Note
First sample:
Second sample:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;
typedef long long ll;
vector <int> a,b;
int main( )
{
int n,n1,n2,x,y;
while(cin >> n){
a.clear(),b.clear();
cin >>n1;
while(n1--){
cin>>x;
a.insert(a.begin(),1,x);//从头压入
//insert(地址loc,数量n,数字x)意思是在loc前面插入n个x
//push_back是从尾部压入
}
cin >>n2;
while(n2--){
cin>>y;
b.insert(b.begin(),1,y);
}
int ans = 0;//ans是次数
int p = 300,p1,p2,q=0;//q是胜者
while(p--){
p1 = a.back(),p2=b.back();
if(p1>p2){
a.pop_back(),b.pop_back();
a.insert(a.begin(),1,p2);
a.insert(a.begin(),1,p1);
ans++;
if(b.empty()){
q = 1;
break;
}
p1 = a.back(),p2=b.back();//back返回尾部元素
}
else{
a.pop_back(),b.pop_back();//pop_back删除尾部元素
b.insert(b.begin(),1,p1);
b.insert(b.begin(),1,p2);
ans++;
if(a.empty()){
q = 2;
break;
}
p1 = a.back(),p2=b.back();
}
}
if(!q)//如果循环300次还没结果,就不行,过程中比较是否出现相同的情况太复杂了
cout<<"-1"<<endl;
else
cout<<ans<<' '<<q<<endl;
}
return 0;
}
上一篇: 回文质数判断