Skip to content

Commit 9b6dc6c

Browse files
authored
Merge pull request #473 from AlgorithmWithGod/khj20006
[20250715] BOJ / P3 / 자리 바꾸기 / 권혁준
2 parents acc5d67 + 32791ed commit 9b6dc6c

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
class Node {
51+
int x, y, z;
52+
Node(int x, int y, int z) {
53+
this.x = x;
54+
this.y = y;
55+
this.z = z;
56+
}
57+
58+
}
59+
60+
public class Main {
61+
62+
static IOController io;
63+
64+
//
65+
66+
static int N, K;
67+
static int[] a;
68+
static int[] cnt;
69+
static int[][] dist;
70+
71+
static long ans = 0;
72+
73+
public static void main(String[] args) throws Exception {
74+
75+
io = new IOController();
76+
77+
init();
78+
solve();
79+
80+
io.close();
81+
82+
}
83+
84+
public static void init() throws Exception {
85+
86+
N = io.nextInt();
87+
K = io.nextInt();
88+
a = new int[N];
89+
cnt = new int[K+1];
90+
dist = new int[K+1][N];
91+
for(int i=0;i<N;i++) {
92+
a[i] = io.nextInt();
93+
dist[a[i]][0] += i - cnt[a[i]];
94+
cnt[a[i]]++;
95+
}
96+
97+
}
98+
99+
static void solve() throws Exception {
100+
101+
for(int k=1;k<=K;k++) {
102+
int x = dist[k][0];
103+
int c = cnt[k];
104+
for(int i=1;i<N;i++) {
105+
x -= c;
106+
if(a[i] == k) c--;
107+
dist[k][i] = x;
108+
}
109+
}
110+
111+
ans = Long.MAX_VALUE;
112+
perm(0, 0, new ArrayList<>());
113+
io.write(ans + "\n");
114+
115+
}
116+
117+
static void perm(int count, int state, List<Integer> list) {
118+
if(count == K) {
119+
int idx = 0;
120+
long res = 0;
121+
for(int i:list) {
122+
res += dist[i][idx];
123+
idx += cnt[i];
124+
}
125+
ans = Math.min(ans, res);
126+
return;
127+
}
128+
for(int i=1;i<=K;i++) if((state & (1<<i)) == 0) {
129+
list.add(i);
130+
perm(count+1, state | (1<<i), list);
131+
list.remove(list.size()-1);
132+
}
133+
}
134+
135+
}
136+
```

0 commit comments

Comments
 (0)