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 <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstring>
#include <unordered_map>
using namespace std;
typedef long long ll;
ll n, m;
ll check(ll x, ll y, vector<vector<ll>> &a, ll ok){
ll res = 0;
if(ok == 1){
res += ((x > 0 && y > 0 && x <= n && y <= m) ? a[x][y] : -1e9);
res += ((x - 1 > 0 && y > 0 && x - 1 <= n && y <= m) ? a[x - 1][y] : -1e9);
res += ((x > 0 && y - 1 > 0 && x <= n && y - 1 <= m) ? a[x][y - 1] : -1e9);
res += ((x > 0 && y + 1 > 0 && x <= n && y + 1 <= m) ? a[x][y + 1] : -1e9);
}
else if(ok == 2){
res += ((x > 0 && y > 0 && x <= n && y <= m) ? a[x][y] : -1e9);
res += ((x - 1 > 0 && y > 0 && x - 1 <= n && y <= m) ? a[x - 1][y] : -1e9);
res += ((x + 1 > 0 && y > 0 && x + 1 <= n && y <= m) ? a[x + 1][y] : -1e9);
res += ((x > 0 && y + 1 > 0 && x <= n && y + 1 <= m) ? a[x][y + 1] : -1e9);
}
else if(ok == 3){
res += ((x > 0 && y > 0 && x <= n && y <= m) ? a[x][y] : -1e9);
res += ((x > 0 && y - 1 > 0 && x <= n && y - 1 <= m) ? a[x][y - 1] : -1e9);
res += ((x + 1 > 0 && y > 0 && x + 1 <= n && y <= m) ? a[x + 1][y] : -1e9);
res += ((x > 0 && y + 1 > 0 && x <= n && y + 1 <= m) ? a[x][y + 1] : -1e9);
}
else if(ok == 4){
res += ((x > 0 && y > 0 && x <= n && y <= m) ? a[x][y] : -1e9);
res += ((x - 1 > 0 && y > 0 && x - 1 <= n && y <= m) ? a[x - 1][y] : -1e9);
res += ((x + 1 > 0 && y > 0 && x + 1 <= n && y <= m) ? a[x + 1][y] : -1e9);
res += ((x > 0 && y - 1 > 0 && x <= n && y - 1 <= m) ? a[x][y - 1] : -1e9);
}
return (res < 0 ? -1e9 : res);
}
ll t[10007];
vector<pair<ll, ll>> d;
vector<vector<ll>>a;
ll res = -1e9;
void put(ll pos, ll val, ll& mx){
t[pos] = val;
if(pos == mx){
set<pair<int, int>> st;
bool ok = false;
ll sum = 0;
for(int i = 1; i <= pos; i++){
if(st.count({d[i - 1].first, d[i - 1].second})){
ok = true;
break;
}
st.insert({d[i - 1].first, d[i - 1].second});
if(t[i] == 1){
if(st.count({d[i - 1].first - 1, d[i - 1].second}) || st.count({d[i - 1].first, d[i - 1].second - 1}) || st.count({d[i - 1].first, d[i - 1].second + 1})){
ok = true;
break;
}
st.insert({d[i - 1].first - 1, d[i - 1].second});
st.insert({d[i - 1].first, d[i - 1].second - 1});
st.insert({d[i - 1].first, d[i - 1].second + 1});
}
else if(t[i] == 2){
if(st.count({d[i - 1].first - 1, d[i - 1].second}) || st.count({d[i - 1].first + 1, d[i - 1].second}) || st.count({d[i - 1].first, d[i - 1].second + 1})){
ok = true;
break;
}
st.insert({d[i - 1].first - 1, d[i - 1].second});
st.insert({d[i - 1].first + 1, d[i - 1].second});
st.insert({d[i - 1].first, d[i - 1].second + 1});
}
else if(t[i] == 3){
if(st.count({d[i - 1].first, d[i - 1].second - 1}) || st.count({d[i - 1].first, d[i - 1].second + 1}) || st.count({d[i - 1].first + 1, d[i - 1].second})){
ok = true;
break;
}
st.insert({d[i - 1].first, d[i - 1].second - 1});
st.insert({d[i - 1].first, d[i - 1].second + 1});
st.insert({d[i - 1].first + 1, d[i - 1].second});
}
else{
if(st.count({d[i - 1].first - 1, d[i - 1].second}) || st.count({d[i - 1].first + 1, d[i - 1].second}) || st.count({d[i - 1].first, d[i - 1].second - 1})){
ok = true;
break;
}
st.insert({d[i - 1].first - 1, d[i - 1].second});
st.insert({d[i - 1].first + 1, d[i - 1].second});
st.insert({d[i - 1].first, d[i - 1].second - 1});
}
if(check(d[i - 1].first, d[i - 1].second, a, t[i]) < 0){
ok = true;
break;
}
sum += check(d[i - 1].first, d[i - 1].second, a, t[i]);
}
if(!ok){
res = max(res, sum);
}
}
else
for(int i = 1; i <= 4; i++)
put(pos + 1, i, mx);
}
int main(){
cin >> n >> m;
a.resize(n + 1, vector<ll>(m + 1));
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> a[i][j];
ll k;
cin >> k;
d.resize(k);
for(int i = 0; i < k; i++){
cin >> d[i].first >> d[i].second;
d[i].first++;
d[i].second++;
}
bool ok = false;
for(int i = 0; i < k; i++)
for(int j = i + 1; j < k; j++)
if(abs(d[i].first - d[j].first) <= 2 && abs(d[i].second - d[j].second) <= 2)
ok = true;
vector<vector<ll>>used(n + 1, vector<ll>(m + 1));
for(auto i: d)
used[i.first][i.second] = 1;
if(!ok){
ll ans = 0;
for(auto i: d){
ll cnt = max({check(i.first, i.second, a, 1),
check(i.first, i.second, a, 2),
check(i.first, i.second, a, 3),
check(i.first, i.second, a, 4)});
if(cnt == -1e9){
cout << "No" << endl;
return 0;
}
ans += cnt;
}
cout << ans << endl;
}
else{
ll ans = 0;
ok = false;
for(int i = 0; i < k; i++)
for(int j = i + 1; j < k; j++)
if(abs(d[i].first - d[j].first) <= 2 && abs(d[i].second - d[j].second) <= 2){
if(abs(d[i].first - d[j].first) == 0 && abs(d[i].second - d[j].second) == 1)
continue;
if(abs(d[i].first - d[j].first) == 1 && abs(d[i].second - d[j].second) == 0)
continue;
if(abs(d[i].first - d[j].first) == 1 && abs(d[i].second - d[j].second) == 1)
continue;
ok = true;
}
if(ok){
cout << "No" << endl;
return 0;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(used[i][j]){
if(j + 1 <= m && used[i][j + 1]){
ll cnt = check(i, j, a, 4) + check(i, j + 1, a, 2);
if(cnt < 0){
cout << "No" << endl;
return 0;
}
ans += cnt;
}
else if(i + 1 <= n && used[i + 1][j]){
ll cnt = check(i, j, a, 1) + check(i + 1, j, a, 3);
if(cnt < 0){
cout << "No" << endl;
return 0;
}
ans += cnt;
}
else if((i - 1 <= 0 || !used[i - 1][j]) && (j - 1 <= 0 || !used[i][j - 1])
&& (i - 1 <= 0 || j - 1 <= 0 || !used[i - 1][j - 1]) &&
(j + 1 > m || i - 1 <= 0 || !used[i - 1][j + 1])){
if(i + 1 <= n && j + 1 <= m && used[i + 1][j + 1]){
ll cnt = check(i, j, a, 1) + check(i + 1, j + 1, a, 3);
if(cnt < 0){
cout << "No" << endl;
return 0;
}
ans += cnt;
continue;
}
if(i + 1 <= n && j - 1 > 0 && used[i + 1][j - 1]){
ll cnt = check(i, j, a, 1) + check(i + 1, j - 1, a, 3);
if(cnt < 0){
cout << "No" << endl;
return 0;
}
ans += cnt;
continue;
}
ll cnt = max({check(i, j, a, 1),
check(i, j, a, 2),
check(i, j, a, 3),
check(i, j, a, 4)});
if(cnt < 0){
cout << "No" << endl;
return 0;
}
ans += cnt;
}
}
}
}
cout << ans << endl;
return 0;
}
if(k <= 10){
for(int i = 1; i <= 4; i++)
put(1, i, k);
if(res == -1e9)
cout << "No" << endl;
else
cout << res << endl;
return 0;
}
return 0;
}
/*
4 4
1 1 1 1
2 2 2 2
1 1 2 2
1 1 2 2
2
1 1
1 2
*/
# | 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... |