天天看點

Codeforces - 546C. Soldier and Cards- Java題解

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, 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 many fights will happen and who will 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 soldier's cards. Then follow k1 integers that are the values on the first soldier's cards, from top to bottom of his stack.

Third line contains integer k2 (k1 + k2 = n), the number of the second soldier's cards. Then follow k2 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  - 1.

Sample test(s) input

4
2 1 3
2 4 2
      

output

6 2      

input

3
1 2
2 1 3
      

output

-1      

Note

First sample:

Codeforces - 546C. Soldier and Cards- Java題解

Second sample:

Codeforces - 546C. Soldier and Cards- Java題解

題目不算太難,主要考小算法和代碼功力,我是使用Java寫的,三個小算法:

1. 使用所謂的two-point方法,用一個數組模拟循環數列,two-point方法簡單說就是用前後兩點對數組進行定點

2. 模拟遊戲去玩,因為資料不大,模拟速度也是很快的

3. 利用hash表的方法記錄遊戲過的狀态,比如記錄10個數字組成的數列作為一個狀态,可以使用原始的數組作為一個狀态,也可以把數字轉換成字元串,最好的方法還是直接轉換成一個整數記錄狀态,因為10個數字組合起來成最大的整數不會超過一個長整形

import java.util.HashMap;
import java.util.Scanner;

public class C546_2 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt(); int k_1; int k_2;
		
		long begin_1 = 0;		
		k_1 = in.nextInt();
		int k_1_arr[] = new int[n];		
		for (int i = 0 ; i < k_1; i++){
			k_1_arr[i] = in.nextInt();
			begin_1 = begin_1 * 10 + k_1_arr[i];
		}
		
		long begin_2 = 0;
		k_2 = in.nextInt();
		int k_2_arr[] = new int[n];
		for (int i = 0; i < k_2; i++){
			k_2_arr[i] = in.nextInt();
			begin_2 = begin_2 * 10 + k_2_arr[i];
		}
				
		int c = 0, front_1 = 0, end_1 = k_1, front_2 = 0, end_2 = k_2;
		int win_side = -1;
		
		HashMap<Long, Long> states = new HashMap<Long, Long>();
		states.put(begin_1, begin_2);
		
		while (true){
			/// judge winner
			if (0 == k_1) {
				win_side = 2;
				break;
			} else if (0 == k_2) {
				win_side = 1;
				break;
			}
			
			/// mimic the game by code!
			++c;
			if (front_1 >= n) front_1 -= n;
			if (front_2 >= n) front_2 -= n;
			if (k_1_arr[front_1] > k_2_arr[front_2]){
				if (end_1 >= n) end_1 -= n;
				k_1_arr[end_1++] = k_2_arr[front_2++];
				if (end_1 >= n) end_1 -= n;
				k_1_arr[end_1++] = k_1_arr[front_1++];
				k_1++; k_2--;
			} else {
				if (end_2 >= n) end_2 -= n;
				k_2_arr[end_2++] = k_1_arr[front_1++];
				if (end_2 >= n) end_2 -= n;
				k_2_arr[end_2++] = k_2_arr[front_2++];
				k_1--; k_2++;
			}
			
			/// handle not solution, a tie
			long last_1 = 0;
			for (int i = 0, j = front_1; i < k_1; i++, j++){
				if (j >= n) j -= n;
				last_1 = last_1 * 10 + k_1_arr[j];
			}
			long last_2 = 0L;
			for (int i = 0, j = front_2; i < k_2; i++, j++) {
				if (j >= n) j -= n;
				last_2 = last_2 * 10 + k_2_arr[j];
			}
			Long v = states.get(last_1);
			
			if (null != v && v.longValue() == last_2) break;
			states.put(last_1, last_2);
		}
		
		if (-1 == win_side) { System.out.println(-1); }
		else{
			System.out.print(c+' '+win_side);
		}
	}
}