1 /* 2 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 3 queue容器:模拟上述过程,当次数达到最大值时判断为-1 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #include <stack> 11 #include <cmath> 12 #include <queue> 13 using namespace std; 14 15 const int MAXN = 20; 16 const int INF = 0x3f3f3f3f; 17 int x[MAXN]; 18 queue<int> Q1; 19 queue<int> Q2; 20 21 int main(void) //Codeforces Round #304 (Div. 2) C. Soldier and Cards 22 { 23 int n, m1, m2; 24 while (scanf ("%d", &n) == 1) 25 { 26 while (!Q1.empty ()) Q1.pop (); 27 while (!Q2.empty ()) Q2.pop (); 28 29 int x; 30 scanf ("%d", &m1); 31 for (int i=1; i<=m1; ++i) 32 { 33 scanf ("%d", &x); Q1.push (x); 34 } 35 36 scanf ("%d", &m2); 37 for (int i=1; i<=m2; ++i) 38 { 39 scanf ("%d", &x); Q2.push (x); 40 } 41 42 int cnt = 0; 43 while (!Q1.empty () && !Q2.empty ()) 44 { 45 int x = Q1.front (); int y = Q2.front (); 46 Q1.pop (); Q2.pop (); 47 if (x < y) {Q2.push (x); Q2.push (y);} 48 else {Q1.push (y); Q1.push (x);} 49 ++cnt; 50 if (cnt == 10000) break; 51 } 52 53 if (cnt == 10000) puts ("-1"); 54 else 55 { 56 printf ("%d %d\n", cnt, (Q1.empty ()) ? 2 : 1); 57 } 58 } 59 60 return 0; 61 } 62 63 /* 64 4 65 2 1 3 66 2 4 2 67 3 68 1 2 69 2 1 3 70 */
时间: 2024-10-14 23:40:42