0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

P4158 [SCOI2009]粉刷匠

https://www.luogu.com.cn/record/86131090
This commit is contained in:
Baoshuo Ren 2022-09-08 19:53:43 +08:00
parent eef109d013
commit 237c953596
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

64
Luogu/P4158/P4158.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <iostream>
#include <utility>
#include <vector>
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<std::pair<int, int>> 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<int>(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;
}