From 237c953596bdc950a23d79605a4611318f1ec1b8 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 8 Sep 2022 19:53:43 +0800 Subject: [PATCH] =?UTF-8?q?P4158=20[SCOI2009]=E7=B2=89=E5=88=B7=E5=8C=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/86131090 --- Luogu/P4158/P4158.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Luogu/P4158/P4158.cpp diff --git a/Luogu/P4158/P4158.cpp b/Luogu/P4158/P4158.cpp new file mode 100644 index 00000000..02921976 --- /dev/null +++ b/Luogu/P4158/P4158.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 55; + +int n, m, t, f[N][N][2], g[2505]; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m >> t; + + for (int i = 1; i <= n; i++) { + int last = 0; + std::string s; + std::vector> blocks; + + cin >> s; + + for (int j = 0; j < m; j++) { + if (s[j] != s[last]) { + blocks.emplace_back(last + 1, j); + last = j; + } + } + + if (last != m) { + blocks.emplace_back(last + 1, m); + } + + for (int i = 1; i <= blocks.size(); i++) { + const auto &block = blocks[i - 1]; + + for (int j = blocks.size(); j; j--) { + // 不染色时直接从前一格转移 + f[i][j][0] = std::max(f[i - 1][j][0], f[i - 1][j][1]); + + // 刷成和上个格子一样的颜色 + f[i][j][1] = std::max(f[i - 1][j - 1][0], f[i - 1][j - 1][1]) + block.second - block.first + 1; + + // 刷成和上上个格子一样的颜色 + if (i >= 2) { + f[i][j][1] = std::max(f[i][j][1], f[i - 2][j][1] + block.second - block.first + 1); + } + } + } + + for (int j = t; j >= 0; j--) { + for (int k = 0; k <= std::min(j, static_cast(blocks.size())); k++) { + g[j] = std::max(g[j], g[j - k] + std::max(f[blocks.size()][k][0], f[blocks.size()][k][1])); + } + } + } + + cout << g[t] << endl; + + return 0; +}