-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMain.java
48 lines (45 loc) · 1.31 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
private static int N;
private static int M;
private static int[] num;
private static boolean[] visited;
private static ArrayList<Integer> res;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
num = new int[N];
visited = new boolean[N];
sc.nextLine();
for (int i = 0; i < N; i++) {
num[i] = sc.nextInt();
}
Arrays.sort(num);
Arrays.fill(visited, false);
res = new ArrayList<>();
sol(0, 0, res);
}
public static void sol(int depth, int idx, ArrayList<Integer> lst) {
if (depth == M) {
for (int i = 0; i < M; i++) {
System.out.print(lst.get(i) + " ");
}
System.out.println();
return;
}
int before = 0;
for (int i = idx; i < N; i++) {
if (!visited[i] && before != num[i]) {
lst.add(num[i]);
visited[i] = true;
before = num[i];
sol(depth + 1, i + 1, lst);
lst.remove(lst.size() - 1);
visited[i] = false;
}
}
}
}