Queue Soldier and Cards

Source: Internet
Author: User

Soldier and Cards

Topic:

Description

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 Sol Dier ' s cards. Then follow k1 integers that is the values on the first soldier ' s cards, from top to bottom of his s Tack.

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

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             Inputs A total of several
2 1 3 First Enter the first person has a few cards, and then enter the card value of these cards
2 4 2 First enter the second person has a few cards, and then enter the card value of these cards
Output
6 2           output comparisons of times and victories for people
Input
3
1 2
2 1 3
Output
-1

Hint

First Sample:

Second Sample:

Test instructions

Two people each had a pile of cards, and each of them took the top card from his pile and put it on the table.

The card value of the big one first his opponent's card the bottom of his card, and then he put his card on the bottom of his cards,

so loop. If one of the players has a stack that is empty, he loses and another wins.

Ideas:

Put two people's card value into different queue, the first team to compare with the first team, the small value of the team's first put in, the other team end of the team will be the value of the team first team tail.

The team capital of the two teams is deleted. This loops until one of the teams is empty and jumps out of the loop.

Knowledge:

The queue provides the following actions

    1. q.empty ()                  return true if the queue is empty, otherwise false   
    2. q.size ()                   returns the number of elements in the queue   
    3. q.pop ()                    Delete the first element of the queue without returning its value   
    4. Q.front ()                 returns the value of the first element of the team, but does not delete the element   
    5. q.push ()                   Press the new element    in the tail of the team;
    6. Q.back ()                   returns the value of the tail element of the queue, but does not delete the element  
1#include <iostream>2#include <queue>3#include <cstring>4 using namespacestd;5 intMain ()6 {7    intn,k1,k2,i,k,b;8queue<int>c,d;9Cin>>N;TenCin>>K1; One  for(i=0; i<k1;i++) A{cin>>b; - C.push (b);} -Cin>>K2; the  for(i=0; i<k2;i++) -{cin>>b; - D.push (b);} -k=0; +  while(1) -{if(C.empty () | | D.empty ()) Break; +  A if(C.front () >D.front ()) at {C.push (D.front ()); - C.push (C.front ()); - D.pop (); - C.pop (); - } - Else  in {D.push (C.front ()); - D.push (D.front ()); to D.pop (); + C.pop (); - } thek++; * if(k>1e6) ${cout<<"-1"<<Endl;Panax Notoginseng      Break;} -  the } + if(C.empty ()) Acout<<k<<"2"<<Endl; the Else  if(k<=1e6) +cout<<k<<"1"<<Endl; - return 0; $}

Queue Soldier and 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.