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 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 stack 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?≤? k 1? ≤? n?-? 1), the number of the first soldier ' 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? +? k 2? =? N ), the number 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 Test (s) input
42 1 32) 4 2
Output
6 2
Input
31 22 1 3
Output
-1
Note
First Sample:
Second Sample:
Test instructions There were two of them, each person again some cards, the total number of not more than 10, each card has a different value, each person's card is fixed, the card can only choose the lowest mark out, and then compare the size, the big win, get the other side of the card, and get this card and own out of the card, all put to the tail, and so cycle, The last person without a hand loses the game.
Ideas: Direct Tag Status Search
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <stack > #include <queue> #include <map> #include <set> #include <vector> #include <math.h># Include <bitset> #include <list> #include <algorithm> #include <climits>using namespace std;# Define Lson 2*i#define Rson 2*i+1#define LS l,mid,lson#define RS mid+1,r,rson#define Up (i,x,y) for (i=x;i<=y;i++) # Define down (i,x,y) for (i=x;i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define GCD (A, B) __ GCD (A, b) #define LL long long#define N 5000005#define INF 0x3f3f3f3f#define EXP 1e-8#define lowbit (x) (x&-x) const int M od = 1e9+7;map<string,map<string,int> > Vis;int n;int k1,k2;int a[15],b[15],c[15];char s1[15],s2[15];int Main () {int i,j,k; scanf ("%d", &n); scanf ("%d", &K1); for (i = 0; i<k1; i++) {scanf ("%d", &a[i]); C[i] = A[i]; } scanf ("%d", &K2); FoR (i = 0; i<k2; i++) {scanf ("%d", &b[i]); C[k1+i] = B[i]; } sort (C,C+K1+K2); for (i = 0; i<k1; i++) {for (j = 0; j<k1+k2; j + +) {if (A[i]==c[j]) s1[i] = j+ ' 0 '; }} S1[k1] = ' + '; for (i = 0; i<k2; i++) {for (j = 0; j<k1+k2; j + +) {if (B[i]==c[j]) s2[i] = j+ ' 0 '; }} S2[k2] = ' + '; VIS[S1][S2] = 1; int ans = 0; while (K1&&K2) {int p1 = S1[0],P2 = S2[0]; if (P1>P2) {for (i = 0; i<k2; i++) s2[i] = s2[i+1]; k2--; for (i = 0; i<k1; i++) s1[i] = s1[i+1]; S1[k1-1] = p2; S1[K1] = p1; k1++; S2[K2] = s1[k1] = ' + '; } else {for (i = 0; i<k1; i++) s1[i] = s1[i+1]; k1--; for (i = 0; i<k2; i++) s2[i] = s2[i+1]; S2[k2-1] = p1; S2[K2] = p2; k2++; S2[K2] = s1[k1] = ' + '; } if (Vis[s1][s2]) {ans =-1; Break } ans++; VIS[S1][S2] = 1; } printf ("%d", ans); if (ans!=-1) {if (K1) printf ("1"); else printf ("2"); } printf ("\ n"); return 0;}
Codeforces546c:soldier and Cards