#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
const int infinity = 1e9;
vvi board, ind;
vii special;
int dir_x[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dir_y[] = {0, 0, -1, 1, 1, -1, 1, -1};
set<pii> q;
int ans = 0;
int Dfs(vvi &graph, int node, vb &visited) {
int x = special[node].x, y = special[node].y;
q.insert({x - 1, y}), q.insert({x + 1, y}), q.insert({x, y - 1}), q.insert({x, y + 1});
ans += board[x][y];
visited[node] = true;
int sz = 1;
for (int neighbor : graph[node]) {
if (!visited[neighbor]) sz += Dfs(graph, neighbor, visited);
}
return sz;
}
int main() {
int n, m;
cin >> n >> m;
board.resize(n, vi(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> board[i][j];
}
}
ind.resize(n, vi(m));
vvb block(n, vb(m));
int k;
cin >> k;
special.resize(k);
for (int i = 0; i < k; i++) {
cin >> special[i].x >> special[i].y;
ind[special[i].x][special[i].y] = i + 1;
block[special[i].x][special[i].y] = true;
}
vb finished(k);
while (true) {
bool is_new = false;
for (int i = 0; i < k; i++) {
if (finished[i]) continue;
int x = special[i].x, y = special[i].y;
int neighbor1 = 4, sum = board[x][y];
for (int j = 0; j < 4; j++) {
x += dir_x[j], y += dir_y[j];
if (0 <= x && x < n && 0 <= y && y < m && !block[x][y]) {
neighbor1--;
sum += board[x][y];
}
x -= dir_x[j], y -= dir_y[j];
}
int neighbor2 = 0;
for (int j = 4; j < 8; j++) {
x += dir_x[j], y += dir_y[j];
if (0 <= x && x < n && 0 <= y && y < m && ind[x][y]) {
neighbor2++;
}
x -= dir_x[j], y -= dir_y[j];
}
if (neighbor1 > 1 || neighbor2 > 1) {
cout << "No";
return 0;
}
if (neighbor1 == 1) {
ans += sum;
finished[i] = true;
is_new = true;
for (int j = 0; j < 4; j++) {
x += dir_x[j], y += dir_y[j];
if (0 <= x && x < n && 0 <= y && y < m) block[x][y] = true;
x -= dir_x[j], y -= dir_y[j];
}
continue;
}
if (neighbor2) {
block[x][y - 1] = true;
block[x][y + 1] = true;
ans += board[x][y - 1] + board[x][y + 1] + board[x][y];
if (ind[x + 1][y + 1] || ind[x + 1][y - 1]) {
block[x - 1][y] = true;
ans += board[x - 1][y];
} else {
block[x + 1][y] = true;
ans += board[x + 1][y];
}
finished[i] = true;
is_new = true;
}
}
if (!is_new) {
break;
}
}
vvi graph(k);
for (int i = 0; i < k; i++) {
if (finished[i]) continue;
int x = special[i].x, y = special[i].y;
if (x > 1 && ind[x - 2][y]) {
graph[i].push_back(ind[x - 2][y] - 1);
}
if (y > 1 && ind[x][y - 2]) {
graph[i].push_back(ind[x][y - 2] - 1);
}
if (x < n - 2 && ind[x + 2][y]) {
graph[i].push_back(ind[x + 2][y] - 1);
}
if (y < m - 2 && ind[x][y + 2]) {
graph[i].push_back(ind[x][y + 2] - 1);
}
}
vb visited(k);
for (int i = 0; i < k; i++) {
if (finished[i] || visited[i]) continue;
q.clear();
int sz = Dfs(graph, i, visited);
if (sz * 3 > q.size()) {
cout << "No";
return 0;
}
int min_val = infinity;
for (pii p : q) {
ans += board[p.x][p.y];
chkmin(min_val, board[p.x][p.y]);
}
if (sz * 3 < q.size()) {
ans -= min_val;
}
}
cout << ans;
}
Compilation message
covering.cpp: In function 'int main()':
covering.cpp:139:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | if (sz * 3 > q.size()) {
| ~~~~~~~^~~~~~~~~~
covering.cpp:148:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
148 | if (sz * 3 < q.size()) {
| ~~~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
8 ms |
460 KB |
Output is correct |
3 |
Correct |
27 ms |
1052 KB |
Output is correct |
4 |
Correct |
91 ms |
2764 KB |
Output is correct |
5 |
Correct |
250 ms |
8388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
332 KB |
Output is correct |
2 |
Correct |
8 ms |
460 KB |
Output is correct |
3 |
Correct |
25 ms |
1068 KB |
Output is correct |
4 |
Correct |
85 ms |
2768 KB |
Output is correct |
5 |
Correct |
252 ms |
8320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
8 ms |
460 KB |
Output is correct |
3 |
Correct |
27 ms |
1052 KB |
Output is correct |
4 |
Correct |
75 ms |
2760 KB |
Output is correct |
5 |
Correct |
247 ms |
8388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
14 ms |
804 KB |
Output is correct |
4 |
Correct |
10 ms |
588 KB |
Output is correct |
5 |
Correct |
32 ms |
1468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
8 ms |
460 KB |
Output is correct |
3 |
Correct |
27 ms |
1052 KB |
Output is correct |
4 |
Correct |
91 ms |
2764 KB |
Output is correct |
5 |
Correct |
250 ms |
8388 KB |
Output is correct |
6 |
Correct |
4 ms |
332 KB |
Output is correct |
7 |
Correct |
8 ms |
460 KB |
Output is correct |
8 |
Correct |
25 ms |
1068 KB |
Output is correct |
9 |
Correct |
85 ms |
2768 KB |
Output is correct |
10 |
Correct |
252 ms |
8320 KB |
Output is correct |
11 |
Correct |
3 ms |
332 KB |
Output is correct |
12 |
Correct |
8 ms |
460 KB |
Output is correct |
13 |
Correct |
27 ms |
1052 KB |
Output is correct |
14 |
Correct |
75 ms |
2760 KB |
Output is correct |
15 |
Correct |
247 ms |
8388 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
2 ms |
204 KB |
Output is correct |
18 |
Correct |
14 ms |
804 KB |
Output is correct |
19 |
Correct |
10 ms |
588 KB |
Output is correct |
20 |
Correct |
32 ms |
1468 KB |
Output is correct |
21 |
Correct |
3 ms |
332 KB |
Output is correct |
22 |
Correct |
8 ms |
652 KB |
Output is correct |
23 |
Correct |
26 ms |
1440 KB |
Output is correct |
24 |
Correct |
91 ms |
3516 KB |
Output is correct |
25 |
Correct |
252 ms |
9216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
8 ms |
460 KB |
Output is correct |
3 |
Correct |
27 ms |
1052 KB |
Output is correct |
4 |
Correct |
91 ms |
2764 KB |
Output is correct |
5 |
Correct |
250 ms |
8388 KB |
Output is correct |
6 |
Correct |
4 ms |
332 KB |
Output is correct |
7 |
Correct |
8 ms |
460 KB |
Output is correct |
8 |
Correct |
25 ms |
1068 KB |
Output is correct |
9 |
Correct |
85 ms |
2768 KB |
Output is correct |
10 |
Correct |
252 ms |
8320 KB |
Output is correct |
11 |
Correct |
3 ms |
332 KB |
Output is correct |
12 |
Correct |
8 ms |
460 KB |
Output is correct |
13 |
Correct |
27 ms |
1052 KB |
Output is correct |
14 |
Correct |
75 ms |
2760 KB |
Output is correct |
15 |
Correct |
247 ms |
8388 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
2 ms |
204 KB |
Output is correct |
18 |
Correct |
14 ms |
804 KB |
Output is correct |
19 |
Correct |
10 ms |
588 KB |
Output is correct |
20 |
Correct |
32 ms |
1468 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
3 ms |
332 KB |
Output is correct |
27 |
Correct |
8 ms |
652 KB |
Output is correct |
28 |
Correct |
26 ms |
1440 KB |
Output is correct |
29 |
Correct |
91 ms |
3516 KB |
Output is correct |
30 |
Correct |
252 ms |
9216 KB |
Output is correct |
31 |
Correct |
607 ms |
34724 KB |
Output is correct |
32 |
Correct |
248 ms |
13780 KB |
Output is correct |
33 |
Correct |
282 ms |
17612 KB |
Output is correct |
34 |
Correct |
256 ms |
13388 KB |
Output is correct |
35 |
Correct |
355 ms |
26988 KB |
Output is correct |