結果
| 問題 | No.133 カードゲーム | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-02-11 17:57:59 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 130 ms / 5,000 ms | 
| コード長 | 3,935 bytes | 
| コンパイル時間 | 4,224 ms | 
| コンパイル使用メモリ | 81,024 KB | 
| 実行使用メモリ | 41,648 KB | 
| 最終ジャッジ日時 | 2024-06-25 03:42:58 | 
| 合計ジャッジ時間 | 7,992 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 19 | 
ソースコード
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);
		}
	}
}
            
            
            
        