Codeforces546C:Soldier and Cards 解题心得

原题:

Description

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to nall 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 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:

 

 
 
 
我的代码:
 1 #include<iostream>
 2 #include<set>
 3 #include<queue>
 4 
 5 
 6 using namespace std;
 7 #define  di queue<int>
 8 
 9 di card1;
10 di card2;
11 int k1[12];
12 int k2[12];
13 set<di> vis;
14 
15 void move(di &a,di&b)
16 {
17     if (a.front() > b.front())
18     {
19         a.push(b.front());
20         b.pop();
21         a.push(a.front());
22         a.pop();
23     }
24     else
25     {
26         b.push(a.front());
27         a.pop();
28         b.push(b.front());
29         b.pop();
30     }
31 }
32 
33 
34 
35 int main()
36 {
37 
38     int n, n1, n2;
39     cin >> n;
40     cin >> n1;
41     for (int i = 0; i < n1; i++)
42     {
43         cin >> k1[i];
44         card1.push(k1[i]);
45     }
46     cin >> n2;
47     for (int i = 0; i < n2; i++)
48     {
49         cin >> k2[i];
50         card2.push(k2[i]);
51     }
52 
53     int cycle = 0;
54     int win = 0;
55     int ans = 0;
56     while (1)
57     {
58         if (ans == 1e4)
59         {
60             cycle = 1;
61             ans = -1;
62             break;
63         }
64         move(card1, card2);
65         ans++;
66         if (card1.empty() )
67         {
68             cycle = 0;
69             win = 2;
70             break;
71         }
72         if (card2.empty())
73         {
74             cycle = 0;
75             win = 1;
76             break;
77         }
78     }
79     if (cycle)
80     {
81         cout << ans << endl;
82     }
83     else
84     {
85         cout << ans <<' '<<win<< endl;
86     }
87     return 0;
88 }
View Code

 

posted @ 2015-07-26 18:37  Shawn_Ji  阅读(241)  评论(0编辑  收藏  举报