From 23c99fc0de357df0a182c793aff1eae0aac51427 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 15 Jun 2022 11:15:40 +0800 Subject: [PATCH] =?UTF-8?q?P3694=20=E9=82=A6=E9=82=A6=E7=9A=84=E5=A4=A7?= =?UTF-8?q?=E5=90=88=E5=94=B1=E7=AB=99=E9=98=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/77411977 --- Luogu/P3694/P3694.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Luogu/P3694/P3694.cpp diff --git a/Luogu/P3694/P3694.cpp b/Luogu/P3694/P3694.cpp new file mode 100644 index 00000000..14e60633 --- /dev/null +++ b/Luogu/P3694/P3694.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +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 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::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; +}