题解 | 小红的矩阵染色

小红的矩阵染色

https://www.nowcoder.com/practice/f8b771318bb04490b7389cc35e148166

#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m, k, score = 0;
    cin >> n >> m >> k;
    vector<string> vec(n);
    for (int i = 0; i < n; i++)
        cin >> vec[i];
    vector<int> white;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (white.empty())
                white.push_back(0);
            if (vec[j][i] == '*') {
                if (white.back() < 2)
                    white.pop_back();
                white.push_back(0);
                continue;
            }
            white.back()++;
        }
        if (!white.empty()&&white.back() < 2)
            white.pop_back();
        white.push_back(0);
    }
    white.pop_back();
    sort(white.begin(), white.end(), [](auto const & a, auto const & b) {
        return a > b;
    });
    for (int num : white) {
        if (num >= k){
            score += k - 1;
            break;
            }
        else {
            k -= num;
            score += num - 1;
        }
    }
    cout << score;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务