Test instructions
two bored soldiers is playing card war. Their card deck consists of Exactly n cards, numbered From 1 to n , All values is different. 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 St Ack. 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 Span class= "Apple-converted-space" > (2≤ n ≤10), the number of cards.
second line contains Integer K 1 (1≤ k 1≤ n -1), the number of the first soldier ' s cards. Then Follow K 1 integers that is the values on the first soldier ' s cards, from top to bottom O F his stack.
third line contains Integer k 2 ( K 1 + k 2 = n ), the number of the second soldier ' s cards . Then Follow K 2 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& nbsp 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.
Input
2 1 3
2 4 2
Output
6 2
Ideas:
Use the queue to simulate two players to play cards, compare the top size of two players, until there is no player card. There is also a tie, set directly to the Loop 1000 times after the result is not a loop.
Source:
1#include <iostream>2#include <queue>3#include <cstring>4 using namespacestd;5queue<int>P1, p2;6 intMain ()7 {8 intT;9CIN >>T;Ten intCount =0; One inta1, A2; A inti =0; - intN; - intm; theCIN >>N; - for(i =0; i<n; i++) - { -CIN >>A1; + P1.push (A1); - } +CIN >>m; A for(i =0; i<m; i++) at { -CIN >>A2; - P2.push (A2); - } - while(1) - { in - if(P1.empty () | | p2.empty ()) Break; to intp =P1.front (); + intQ =P2.front (); - P1.pop (); the P2.pop (); * $ if(P >q)Panax Notoginseng { - P1.push (q); the P1.push (p); + Acount++; the } + Else - { $ P2.push (p); $ P2.push (q); - -count++; the } - Wuyi if(Count > +) the { -Count =-1; Wu Break; - About } $ } - - if(P1.empty () | |p2.empty ()) - { A +cout << Count <<" "<< (p1.empty)?2:1) <<Endl; the } - Else $ { thecout <<"-1"<<Endl; the } the the return 0; -}
Experience:
This topic is mainly in looking for loops there card for a long time ... And then find the next lovely degree Niang, see others use of >1e6 .... And then I used it for a bit,
Later when the submission is directly in the first on the timeout, after a long time (in the submission of many times in the first Test after the timeout) ... Directly to the while (t--) removed, the number of cycles >1000 unexpectedly, accidental discovery.
It's enough ... I guess we'll have to learn a little later.
Play Cards ~ ~