読者です 読者をやめる 読者になる 読者になる

yukicoder No. 173: カードゲーム(Medium) (モンテカルロ法・シミュレーション)

yukicoder 競技プログラミング

解法

誤差0.005以内で正解になるという緩さなので、10000100,000回シミュレーションして勝率を出す。


コード

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Random random = new Random();
		int N = sc.nextInt();
		double Pa = sc.nextDouble();
		double Pb = sc.nextDouble();

		int[] a = new int[N];
		int[] b = new int[N];
		for (int i = 0; i < N; i++) {
			a[i] = sc.nextInt();
		}
		for (int i = 0; i < N; i++) {
			b[i] = sc.nextInt();
		}
		sc.close();

		int rep = 100000;

		int wina = 0;
		for (int i = 0; i < rep; i++) {
			int pointa = 0;
			int pointb = 0;

			ArrayList<Integer> aList = new ArrayList<>();
			ArrayList<Integer> bList = new ArrayList<>();
			for (int j = 0; j < N; j++) {
				aList.add(a[j]);
				bList.add(b[j]);
			}
			Collections.sort(aList);
			Collections.sort(bList);
			for (int j = 0; j < N; j++) {
				double rand_a = random.nextDouble();
				double rand_b = random.nextDouble();

				int card_aid = (rand_a < Pa || aList.size() == 1) ? 0 : random.nextInt(aList.size() - 1) + 1;
				int card_bid = (rand_b < Pb || bList.size() == 1) ? 0 : random.nextInt(bList.size() - 1) + 1;
				int card_a = aList.get(card_aid);
				aList.remove(card_aid);
				int card_b = bList.get(card_bid);
				bList.remove(card_bid);

				if (card_a > card_b) {
					pointa += card_a + card_b;
				} else if (card_a < card_b) {
					pointb += card_a + card_b;
				}
			}
			if (pointa > pointb) {
				wina++;
			}
		}
		System.out.println((double) wina / rep);

	}
}