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>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define SZ(x) (int)(x.size())
#define F0(i,n) for(int i=0;i<n;i++)
#define F1(i,n) for(int i=1;i<=n;i++)
#define CL(a,x) memset(x, a, sizeof(x));
#define PR(x) cerr << #x << "=" << (x) << endl;
const int MOD = 1000000007;
const double pi = atan(1.0)*4.0;
const double eps = 1e-8;
const int N = 1000001;
int i, j, k, m, n;
int a[N];
int b[N], d[N], c[N];
ll ans;
const int DX[]={-1,0,1,0,0};
const int DY[]={0,1,0,-1,0};
void nosol() {
cout << "No" << endl;
exit(0);
}
bool Empty(int x, int y) {
if (x < 0 || x >= m || y < 0 || y >= n) return 0;
return !c[x*n + y];
}
int GetA(int x, int y) {
return a[x*n + y];
}
void Fill(int x, int y) {
c[x*n + y] = 1;
ans += GetA(x, y);
}
int main() {
//freopen("x.in", "r", stdin);
cin >> m >> n;
F0(i, m * n) scanf("%d", &a[i]);
cin >> k;
while (k--) {
scanf("%d%d", &i, &j);
b[i*n+j] = 1;
}
// find pairs
F0(i, m) F0(j, n) if (b[i*n + j]) {
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) if (dx || dy) {
int i2 = i + dx, j2 = j + dy;
if (i2 < 0 || i2 >= m || j2 < 0 || j2 >= n || !b[i2*n + j2]) continue;
if (d[i*n + j] && d[i2*n + j2]) continue;
if (d[i*n + j] || d[i2*n + j2]) nosol();
d[i*n + j] = 1;
d[i2*n + j2] = 1;
F0(k, 5) if (!Empty(i + DX[k], j + DY[k])) nosol();
F0(k, 5) if (!Empty(i2 + DX[k], j2 + DY[k])) nosol();
F0(k, 5) if (Empty(i + DX[k], j + DY[k])) Fill(i + DX[k], j + DY[k]);
F0(k, 5) if (Empty(i2 + DX[k], j2 + DY[k])) Fill(i2 + DX[k], j2 + DY[k]);
}
}
}
F0(i, m) F0(j, n) if (b[i*n + j] && !d[i*n + j]) {
if (!Empty(i, j)) nosol();
int bsum = -1;
int bk = -1;
F0(k, 4) {
int good = 1, sum = 0;
F0(dir, 4) if (dir != k) {
if (!Empty(i + DX[dir], j + DY[dir])) {
good = 0;
break;
} else {
sum += GetA(i + DX[dir], j + DY[dir]);
}
}
if (sum > bsum) {
bsum = sum;
bk = k;
}
}
if (bk == -1) nosol();
d[i*n + j] = 1;
F0(dir, 5) if (dir != bk) Fill(i + DX[dir], j + DY[dir]);
}
cout << ans << endl;
return 0;
}
Compilation message (stderr)
covering.cpp: In function 'int main()':
covering.cpp:73:17: warning: variable 'good' set but not used [-Wunused-but-set-variable]
int good = 1, sum = 0;
^~~~
covering.cpp:44:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
F0(i, m * n) scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
covering.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &i, &j);
~~~~~^~~~~~~~~~~~~~~~
# | 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... |