queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

时间:2021-02-02 13:50:14

题目传送门

 /*
题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌
queue容器:模拟上述过程,当次数达到最大值时判断为-1
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f;
int x[MAXN];
queue<int> Q1;
queue<int> Q2; int main(void) //Codeforces Round #304 (Div. 2) C. Soldier and Cards
{
int n, m1, m2;
while (scanf ("%d", &n) == )
{
while (!Q1.empty ()) Q1.pop ();
while (!Q2.empty ()) Q2.pop (); int x;
scanf ("%d", &m1);
for (int i=; i<=m1; ++i)
{
scanf ("%d", &x); Q1.push (x);
} scanf ("%d", &m2);
for (int i=; i<=m2; ++i)
{
scanf ("%d", &x); Q2.push (x);
} int cnt = ;
while (!Q1.empty () && !Q2.empty ())
{
int x = Q1.front (); int y = Q2.front ();
Q1.pop (); Q2.pop ();
if (x < y) {Q2.push (x); Q2.push (y);}
else {Q1.push (y); Q1.push (x);}
++cnt;
if (cnt == ) break;
} if (cnt == ) puts ("-1");
else
{
printf ("%d %d\n", cnt, (Q1.empty ()) ? : );
}
} return ;
} /*
4
2 1 3
2 4 2
3
1 2
2 1 3
*/