-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
75 lines (60 loc) Β· 2.14 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package Permutation.P1722;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static long[] factorial = new long[21];
static int N;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Permutation/P1722/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
factorial[0] = 1;
for (int i = 1; i <= 20 ; i++) {
factorial[i] = factorial[i - 1] * i;
}
N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int command = Integer.parseInt(st.nextToken());
if (command == 1) {
long k = Long.parseLong(st.nextToken());
int[] ans = new int[N];
boolean[] visited = new boolean[N];
for (int i = 0; i < N; i++) {
long target = factorial[N - i - 1];
for (int j = 0; j < N; j++) {
if (visited[j]) {
continue;
}
if (k <= target) {
ans[i] = j + 1;
visited[j] = true;
break;
} else {
k -= target;
}
}
}
for (int i = 0; i < N; i++) {
System.out.print(ans[i] + " ");
}
System.out.println("");
} else if (command == 2) {
int[] nums = new int[N];
boolean[] visited = new boolean[N + 1];
long ans = 0;
for (int i = 0; i < N; i++) {
nums[i] = Integer.parseInt(st.nextToken());
}
for (int i = 0; i < N - 1; i++) {
for (int j = 1; j < nums[i]; j++) {
if (!visited[j]) {
ans += factorial[N - i - 1];
}
}
visited[nums[i]] = true;
}
System.out.println(ans + 1);
}
}
}