Submission #1221572

#TimeUsernameProblemLanguageResultExecution timeMemory
1221572khomeT-Covering (eJOI19_covering)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

void solve(){
    int n, m; cin >> n >> m;
    vector<vector<int>> els(n, vector<int>(m));
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            cin >> els[i][j];
        }
    }
    bool so = true;

    int k; cin >> k;
    vector<pair<int, int>> xs(k);
    for (int i = 0; i < k; i ++){
        int x, y; cin >> x >> y;
        xs[i] = {x, y};
        if (min(x, y) == 0 && (x == n - 1 || y == m - 1)) so = false;
    }

    if (!so) {cout << "No" << endl; return;}

    vector<set<pair<int, int>>> components;
    
    for (auto& pr : xs){
        int x = pr.first, y = pr.second;
        bool est = false;

        for (int i = 0; i < components.size(); i++){
            set<pair<int, int>> st = components[i];
            if (st.find({x-1, y-1}) != st.end()) est = true;
            
            else if (st.find({x+1, y+1}) != st.end()) est = true;
            
            else if (st.find({x-1, y}) != st.end()) est = true;

            else if (st.find({x, y-1}) != st.end()) est = true;

            else if (st.find({x+1, y}) != st.end()) est = true;

            else if (st.find({x, y+1}) != st.end()) est = true;

            else if (st.find({x, y+2}) != st.end()) est = true;

            else if (st.find({x, y-2}) != st.end()) est = true;

            else if (st.find({x+2, y}) != st.end()) est = true;

            else if (st.find({x-2, y}) != st.end()) est = true;

            if (est) {
                components[i].insert({x, y});
                break;
            }
        }

        if (!est) {
            components.push_back({{x, y}});
        }
    }
    
    int ans = 0;
    for (auto smth : components){
        set<vector<int>> sosedi;
        int cnt = 0;
        for (pair<int, int> pr : smth) {
            int x = pr.first, y = pr.second;
            cnt += els[x][y];

            if (x-1 >= 0 && sosedi.find({els[x-1][y], x-1, y}) == sosedi.end()) {
                sosedi.insert({els[x-1][y], x-1, y}); 
                cnt += els[x-1][y];
            }

            if (x+1 < n && sosedi.find({els[x+1][y], x+1, y}) == sosedi.end()) {
                sosedi.insert({els[x+1][y], x+1, y}); 
                cnt += els[x+1][y];
            }

            if (y-1 >= 0 && sosedi.find({els[x][y-1], x, y-1}) == sosedi.end()) {
                sosedi.insert({els[x][y-1], x, y-1}); 
                cnt += els[x][y-1];
            }

            if (y+1 < m && sosedi.find({els[x][y+1], x, y+1}) == sosedi.end()) {
                sosedi.insert({els[x][y+1], x, y+1}); 
                cnt += els[x][y+1];
            }

        }

        if (sosedi.size() < 3*smth.size()) {cout << "No" << endl; return;}

        if (sosedi.size() == 3*smth.size()) ans += cnt;

        if (sosedi.size() == 3*smth.size() + 1) ans += cnt - (*sosedi.begin())[0];
    }
    cout << ans << endl;
    // cout << "YYAY" << endl;

}

int main() {
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
	int t = 1; 
    // cin >> t;
    while (t--)solve();
}


Compilation message (stderr)

cc1plus: error: '::main' must return 'int'