Skip to content

Instantly share code, notes, and snippets.

@sifue
Created July 6, 2016 07:55
Show Gist options
  • Select an option

  • Save sifue/e145bb78f94a37ecec8c26df9817ad81 to your computer and use it in GitHub Desktop.

Select an option

Save sifue/e145bb78f94a37ecec8c26df9817ad81 to your computer and use it in GitHub Desktop.
プログラミングコンテスト系の標準入力からデータを受け取るコード
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = null;
try {
input = new String(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
String[] strs = input.split(" ");
n = Integer.parseInt(strs[0]);
k = Integer.parseInt(strs[1]);
if (k >= n) {
System.out.println("1.000000000000");
return;
}
make_perm(0, new int[n], new boolean[n + 1]);
System.out.println(findCount / (double) count);
}
public static int k = 0;
public static int n = 0;
public static int count = 0;
public static int findCount = 0;
public static void count_perm(int[] perm){
count++;
int maxExp = 0;
int eatMax = 0;
int eatCount = 0;
for (int j = 0; j < perm.length; j++) {
if (0 != j &&
perm[j] > maxExp &&
eatCount < k) {
eatMax = perm[j];
eatCount++;
}
if (perm[j] > maxExp) {
maxExp = perm[j];
}
}
if (eatMax == n) {
findCount++;
}
}
public static void make_perm(int n, int[] perm, boolean[] flag){
if(n == perm.length){
count_perm(perm);
} else {
for(int i = 1; i <= perm.length; i++){
if(flag[i]) continue;
perm[n] = i;
flag[i] = true;
make_perm(n + 1, perm, flag);
flag[i] = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment