Soldiers and cards, soldiers cards

Source: Internet
Author: User

Soldiers and cards, soldiers cards

Two bored soldiers are playing card war. Their card deck consists of exactlyNCards, numbered from 1N, 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 exactly fights will happen and who will win the game, or state that game won't end.

Input

First line contains a single integerN(2 cores ≤ CoresNLimit ≤ limit 10), the number of cards.

Second line contains integerK1 (1 digit ≤ DigitK1 bytes ≤ bytesNRows-limit 1), the number of the first soldier's cards. Then followK1 integers that are the values on the first soldier's cards, from top to bottom of his stack.

Third line contains integerK2 (K1 worker + workerK2 bytes = bytesN), The number of the second soldier's cards. Then followK2 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 limit-limit 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:

Solution:

 

 

Use two stacks to simulate its occurrence.

 

 

 

 

 

 

 

The Code is as follows:

 

 

# Include <iostream>
# Include <queue>
Using namespace std;
Int main ()
{
Int n;
While (cin> n)
{
Int k1, k2, x, time = 0, y, vicw.flag;
Queue <int> q1;
Queue <int> q2;
Cin> k1;
For (int I = 0; I <k1; I ++)
{
Cin> x;
Q1.push (x );
}
Cin> k2;
For (int I = 0; I <k2; I ++)
{
Cin> x;
Q2.push (x );
}
While (1)
{
Flag = 0;
X = q1.front ();
Y = q2.front ();
Q2.pop ();
Q1.pop ();
Time ++;
If (time = 1e4)
Break;
If (x> y)
{
Q1.push (y );
Q1.push (x );
}
Else
{
Q2.push (x );
Q2.push (y );
}
If (q1.size () = 0 | q2.size () = 0)
{
Vicente = (q1.size () = 0? 2: 1 );
Flag = 1;
Break;
}

}
If (flag)
Cout <time <"" <Vicente <endl;
Else
Cout <-1 <endl;
}
Return 0;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.