|
1 | 1 | import java.io.BufferedReader; |
2 | 2 | import java.io.IOException; |
3 | 3 | import java.io.InputStreamReader; |
4 | | -import java.util.ArrayList; |
5 | 4 | import java.util.Arrays; |
6 | | -import java.util.List; |
7 | 5 | import java.util.StringTokenizer; |
8 | 6 |
|
9 | 7 | public class Main { |
10 | | - static int n, m; |
11 | | - static int[] selected, arr; |
12 | | - static int[][] map, dp; |
| 8 | + static int[] arr, selected; |
13 | 9 | static boolean[] visited; |
| 10 | + static int n, m; |
14 | 11 | static StringBuilder sb = new StringBuilder(); |
15 | | - |
16 | 12 | public static void main(String[] args) throws IOException { |
17 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
18 | 14 | StringTokenizer st; |
19 | 15 | st = new StringTokenizer(br.readLine()); |
20 | 16 | n = Integer.parseInt(st.nextToken()); |
21 | 17 | m = Integer.parseInt(st.nextToken()); |
22 | | - visited = new boolean[n + 1]; |
23 | | - selected = new int[m]; |
24 | | - arr = new int[n]; |
| 18 | + |
25 | 19 | st = new StringTokenizer(br.readLine()); |
| 20 | + arr = new int[n]; |
26 | 21 | for (int i = 0; i < n; i++) { |
27 | 22 | arr[i] = Integer.parseInt(st.nextToken()); |
28 | 23 | } |
29 | 24 | Arrays.sort(arr); |
| 25 | + |
| 26 | + selected = new int[m]; |
| 27 | + visited = new boolean[n + 1]; |
30 | 28 | recur(0, 0); |
31 | 29 | System.out.println(sb); |
32 | 30 | } |
33 | 31 |
|
34 | | - static void recur(int depth, int start) { |
35 | | - if (depth == m) { |
| 32 | + static void recur(int cur, int start) { |
| 33 | + if (cur == m) { |
36 | 34 | for (int i : selected) { |
37 | 35 | sb.append(i).append(" "); |
38 | 36 | } |
39 | 37 | sb.append("\n"); |
40 | 38 | return; |
41 | 39 | } |
42 | 40 | for (int i = start; i < n; i++) { |
43 | | - if (visited[i]) continue; |
| 41 | + if (visited[i] == true) continue; |
44 | 42 | visited[i] = true; |
45 | | - selected[depth] = arr[i]; |
46 | | - recur(depth + 1, i + 1); |
| 43 | + selected[cur] = arr[i]; |
| 44 | + recur(cur + 1, i + 1); |
47 | 45 | visited[i] = false; |
48 | 46 | } |
49 | 47 | } |
|
0 commit comments