이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
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;
used.resize(n + 1, vector<ll>(m + 1));
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++;
used[d[i].first][d[i].second] = 1;
}
sort(d.begin(), d.end());
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;
}
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;
}
# | 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... |