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>
using namespace std;
typedef long long ll;
vector<pair<ll, ll>> state(ll x, ll y, ll ok){
if(ok == 1)
return {{x, y}, {x-1, y}, {x, y - 1}, {x, y + 1}};
if(ok == 2)
return {{x, y}, {x-1, y}, {x + 1, y}, {x, y + 1}};
if(ok == 3)
return {{x, y}, {x+1, y}, {x, y - 1}, {x, y + 1}};
return {{x, y}, {x-1, y}, {x + 1, y}, {x, y - 1}};
}
ll n, m;
vector<vector<ll>>used;
ll check(ll x, ll y, vector<vector<ll>>&a, ll ok){
auto d = state(x, y, ok);
ll ans = 0;
for(auto i: d){
if(i.first <= 0 || i.second <= 0 || i.first > n || i.second > m)
return -1e9;
if(i.first != x || i.second != y){
if(used[i.first][i.second] == 1)
return -1e9;
}
ans += a[i.first][i.second];
}
return ans;
}
int main(){
cin >> n >> m;
used.resize(n + 1, vector<ll>(m + 1));
vector<vector<ll>>a(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;
vector<pair<ll, ll>> d(k);
for(int i = 0; i < k; i++){
cin >> d[i].first >> d[i].second;
d[i].first++, d[i].second++;
used[d[i].first][d[i].second] = 1;
}
sort(d.begin(), d.end());
set<ll>st;
for(auto i: d)
st.insert(i.second);
if(st.size() == 1){
vector<vector<ll>>dp(k + 1, vector<ll>(5));
for(int i =0 ; i < k; i++)
for(int j = 1; j <= 4; j++)
dp[i][j] = -1e9;
for(int i = 1; i <= 4; i++){
dp[0][i] = check(d[0].first, d[0].second, a, i);
}
for(int i = 1; i< k; i++){
for(int j = 1; j <= 4; j++){
for(int last = 1; last <= 4; last++){
auto v1 = state(d[i].first, d[i].second, j);
auto v2 = state(d[i - 1].first, d[i - 1].second, last);
set<pair<ll, ll>> st;
bool ok = false;
for(auto l: v1){
if(st.count(l))
ok = true;
st.insert(l);
}
for(auto l: v2){
if(st.count(l))
ok = true;
st.insert(l);
}
if(ok) // overlaping
continue;
dp[i][j] = max(dp[i][j], dp[i - 1][last] + check(d[i].first, d[i].second, a, j));
}
}
}
ll mx = -1;
for(int j = 1; j <= 4; j++)
mx = max(mx, dp[k - 1][j]);
if(mx < 0)
cout << "No" << endl;
else
cout << mx << endl;
return 0;
}
return 0;
}
# | 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... |