This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define all(s) s.begin(), s.end()
#define lb(s, a) lower_bound(all(s), (a)) - s.begin()
#define ii pair <int, int>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int ar = 2e5+5;
const ll mod = 1e9+7;
const int oo = 1e9;
int n, m, k;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "d"
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> m >> n;
vector <vector <int>> a(m + 1, vector <int> (n + 1, 0));
vector <vector <bool>> ok(m + 1, vector <bool> (n + 1, 0));
vector <vector <bool>> vs(m + 1, vector <bool> (n + 1, 0));
for(int i = 1; i <= m; ++i)
for(int j = 1; j <= n; ++j)
cin >> a[i][j];
cin >> k;
int x, y;
for(int i = 1; i <= k; ++i) {
cin >> x >> y;
x++;
y++;
ok[x][y] = 1;
}
int res = 0;
for(int i = 1; i <= m; ++i)
for(int j = 1; j <= n; ++j) if(!vs[i][j] && ok[i][j]) {
vs[i][j] = 1;
queue <ii> q;
q.push({i, j});
vector <int> val;
int cnt = 0;
while(q.size()) {
int u = q.front().fi, v = q.front().se;
q.pop();
if(ok[u][v]) {
cnt++, res += a[u][v];
// cout << u << ' ' << v << '\n';
}
else val.push_back(a[u][v]);
for(int t = 0; t < 4; ++t) {
int x = u + dx[t], y = v + dy[t];
if(x > 0 && y > 0 && x <= m && y <= n && !vs[x][y] && (ok[u][v] || ok[x][y]))
vs[x][y] = 1, q.push({x, y});
}
}
if(val.size() < 3 * cnt) cout << "No", exit(0);
// cout << i << ' ' << j << '\n';
for(auto x : val)
res += x;
if(val.size() > 3 * cnt) res -= (*min_element(all(val)));
// cout << '\n';
}
cout << res;
}
Compilation message (stderr)
covering.cpp: In function 'int main()':
covering.cpp:69:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
69 | if(val.size() < 3 * cnt) cout << "No", exit(0);
| ~~~~~~~~~~~^~~~~~~~~
covering.cpp:74:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
74 | if(val.size() > 3 * cnt) res -= (*min_element(all(val)));
| ~~~~~~~~~~~^~~~~~~~~
covering.cpp:25:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
covering.cpp:26:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |