0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:25:24 +00:00

P3694 邦邦的大合唱站队

https://www.luogu.com.cn/record/77411977
This commit is contained in:
Baoshuo Ren 2022-06-15 11:15:40 +08:00
parent d6aec9bba0
commit 23c99fc0de
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

47
Luogu/P3694/P3694.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
#include <array>
#include <limits>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5,
M = 25;
int n, m, f[(1 << 20) + 5];
std::array<int, M> s[N];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1, x; i <= n; i++) {
cin >> x;
s[i] = s[i - 1];
s[i][x]++;
}
std::fill(f + 1, f + (1 << m), std::numeric_limits<int>::max());
for (int i = 0; i < 1 << m; i++) {
int cnt = 0;
for (int j = 0; j < m; j++) {
if (i & (1 << j)) cnt += s[n][j + 1];
}
for (int j = 0; j < m; j++) {
if (i & (1 << j)) continue;
int l = cnt, r = l + s[n][j + 1];
f[i | (1 << j)] = std::min(f[i | (1 << j)], f[i] + r - l - (s[r][j + 1] - s[l][j + 1]));
}
}
cout << f[(1 << m) - 1] << endl;
return 0;
}