제출 #1149053

#제출 시각아이디문제언어결과실행 시간메모리
1149053phuonglinhn09T-Covering (eJOI19_covering)C++20
15 / 100
1 ms580 KiB
/**
 *   author:   phuonglinhn09
 *   created:  11.02.2025
 */

#include <bits/stdc++.h>
#define fi first
#define se second

using namespace std;

typedef long long ll;

int n, m, k;
vector<vector<int>> a, tplt;
vector<vector<bool>> vis;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

bool check (int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < m && tplt[x][y] != 0 && !vis[x][y];
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;

    a.resize(n + 5);
    tplt.resize(n + 5);
    vis.resize(n + 5);

    for (int i = 0; i < n; i++) {
        a[i].resize(m + 5, 0);
        tplt[i].resize(m + 5, 0);
        vis[i].resize(m + 5, 0);
        
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }

    cin >> k;
    int x, y;
    while (k--) {
        cin >> x >> y;

        tplt[x][y] = 2;

        int row, col;
        for (int i = 0; i < 4; i++) {
            row = x + dx[i], col = y + dy[i];
            if (row >= 0 && row < n && col >= 0 && col < m) {
                tplt[row][col] = 1;
            }
        }
    }

    queue<pair<int, int>> q;
    int sum, mi, cnt1, cnt2;

    int ans = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (tplt[i][j] != 0 && !vis[i][j]) {
                sum = cnt1 = cnt2 = 0;
                mi = 1e9 + 1;

                q.emplace(i, j);
                while (!q.empty()) {
                    auto [row, col] = q.front();
                    q.pop();
                    for (int i = 0; i < 4; i++) {
                        x = row + dx[i], y = col + dy[i];
                        if (check(x, y)) {
                            mi = min (mi, a[x][y]);
                            sum += a[x][y];
                            cnt1 += tplt[x][y] == 1;
                            cnt2 += tplt[x][y] == 2;

                            vis[x][y] = 1;
                            q.emplace(x, y);
                        }
                    }
                }

                if (cnt1 < cnt2 * 3) return cout << "No", 0;
                else if (cnt1 == cnt2 * 3) ans += sum;
                else ans += sum - mi;
            }
        }
    }

    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...