結果

問題 No.133 カードゲーム
ユーザー GrenacheGrenache
提出日時 2016-02-11 17:57:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 107 ms / 5,000 ms
コード長 3,935 bytes
コンパイル時間 3,245 ms
コンパイル使用メモリ 76,592 KB
実行使用メモリ 56,108 KB
最終ジャッジ日時 2023-09-07 09:24:56
合計ジャッジ時間 6,577 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
55,664 KB
testcase_01 AC 99 ms
55,968 KB
testcase_02 AC 107 ms
55,420 KB
testcase_03 AC 101 ms
55,628 KB
testcase_04 AC 104 ms
55,528 KB
testcase_05 AC 107 ms
56,108 KB
testcase_06 AC 99 ms
55,420 KB
testcase_07 AC 103 ms
55,536 KB
testcase_08 AC 90 ms
55,328 KB
testcase_09 AC 100 ms
55,476 KB
testcase_10 AC 99 ms
55,828 KB
testcase_11 AC 98 ms
55,824 KB
testcase_12 AC 101 ms
55,532 KB
testcase_13 AC 102 ms
55,308 KB
testcase_14 AC 99 ms
55,588 KB
testcase_15 AC 98 ms
55,356 KB
testcase_16 AC 101 ms
55,488 KB
testcase_17 AC 94 ms
54,544 KB
testcase_18 AC 86 ms
53,884 KB
testcase_19 AC 96 ms
55,660 KB
testcase_20 AC 103 ms
55,768 KB
testcase_21 AC 105 ms
55,564 KB
testcase_22 AC 86 ms
53,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;


public class Main_yukicoder133 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        int n = sc.nextInt();
        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();
        }

        int win = 0;
        int cnt = 0;
    	Permutation np = new Permutation(n);
    	for (List<Integer> perm : np) {
    		if (perm == null) {
    			break;
    		}
//    		System.out.println(perm);

    		int awin = 0;
    		for (int i = 0; i < n; i++) {
    			if (a[i] > b[perm.get(i)]) {
    				awin++;
    			} else if (a[i] < b[perm.get(i)]) {
    				awin--;
    			}
    		}

    		if (awin > 0) {
    			win++;
    		}
    		cnt++;
    	}

        pr.printf("%.3f\n", (double)win / cnt);

        pr.close();
        sc.close();
    }

    private static class Permutation implements Iterable<List<Integer>>{
    	int n;

    	boolean[] used;
    	List<Integer> perm;
    	Deque<Integer> ist;
    	PermutationIterator it;

    	Permutation(int n) {
    		this.n = n;
    		used = new boolean[n];
    		perm = new ArrayList<Integer>();
    		ist = new ArrayDeque<Integer>();
    		ist.add(-1);
    		it = new PermutationIterator(this);
    	}

    	List<Integer> next() {
    		out:
    			while (!ist.isEmpty()) {
    				int k = ist.pop();

    				if (k != -1) {
    					// ループ途中での戻り
    					used[k] = false;
    					perm.remove(perm.size() - 1);
    				} else {
    					// 最初から
    					int pos = perm.size();

    					if (pos == n) {
    						// perm に対する操作 =========================
    						return perm;
    						// perm に対する操作 =========================
    					}
    				}

    				for (int i = k + 1; i < n; i++) {
    					if (!used[i]) {
    						ist.push(i);

    						used[i] = true;
    						perm.add(i);
    						ist.push(-1);
    						continue out;
    					}
    				}
    			}

    	return null;
    	}

    	@Override
    	public Iterator<List<Integer>> iterator() {
    		return it;
    	}

    	private static class PermutationIterator implements Iterator<List<Integer>> {

    		Permutation p;
    		PermutationIterator(Permutation p) {
    			this.p = p;
    		}

    		@Override
    		public boolean hasNext() {
    			return !p.ist.isEmpty();
    		}

    		@Override
    		public List<Integer> next() {
    			return p.next();
    		}
    	}
    }

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0