ACM Two soldiers playing cards

Source: Internet
Author: User

The bored soldiers is playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values is differ Ent. They divide cards between them in some manner, it's possible that they has different number of cards. Then they play a "war"-like card game.

The rules are following. On each turn a fight happens. Each of the 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 B Ottom of his stack. If after some turn one of the player ' s stacks becomes empty, he loses and the other one wins.

Calculate how many fights would happen and who'll 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 Soldi Er ' s cards. Then follow k1 integers that is the values on the first soldier ' s cards, from top to bottom of his stack .

Third line contains integer k2 (k1 + k2 = n), the Nu Mber of the second soldier ' s cards. Then follow k2 integers that is the values on the second soldier ' s cards, from top to bottom of his Stack.

All card values is 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 would 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

Problem Solving Ideas: This topic we use queue to solve better, we first set up two queues. The key to solving this problem is that each time we take the first element out of the two queues, we determine their size, remember to delete the first element of the team each time, insert the elements of the small team into the tail of the big team, and then plug his own elements into the back, knowing that one of the teams is empty. , where no empty team is the winner. If there are many cycles, there is no change to null. This is the expression that falls into the loop, at which time the output is -1.

Program code:

#include <iostream> #include <queue>using namespace Std;int main () {queue<int>v;      queue<int>w;      int n,x,y,c,t,k=0,first=1;      cin>>n;      cin>>x;            while (x--) {cin>>c;      V.push (c);      } cin>>y;            while (y--) {cin>>c;      W.push (c); } while (!v.empty () &&!w.empty ()) {if (k>1000) {firs                  t=0;            Break                } if (V.front () >w.front ()) {V.push (W.front ());                V.push (V.front ());                V.pop ();                  W.pop ();            k++;                } else {W.push (V.front ());                W.push (W.front ());                W.pop ();                  V.pop ();            k++;      }} t= (V.empty ()? 2:1); if (first) cout<<k<< ""<<t<<endl;            Else cout<< "-1" <<endl;  return 0;}

  

ACM Two soldiers playing cards

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.