Submission #637661

#TimeUsernameProblemLanguageResultExecution timeMemory
637661ksu2009enT-Covering (eJOI19_covering)C++17
0 / 100
3 ms468 KiB
#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; vector<vector<ll>>used; 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 && !used[x - 1][y]) ? a[x - 1][y] : -1e9); res += ((x > 0 && y - 1 > 0 && x <= n && y - 1 <= m && !used[x][y - 2]) ? a[x][y - 1] : -1e9); res += ((x > 0 && y + 1 > 0 && x <= n && y + 1 <= m && !used[x][y + 1]) ? 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 && !used[x - 1][y]) ? a[x - 1][y] : -1e9); res += ((x + 1 > 0 && y > 0 && x + 1 <= n && y <= m && !used[x + 1][y]) ? a[x + 1][y] : -1e9); res += ((x > 0 && y + 1 > 0 && x <= n && y + 1 <= m && !used[x][y + 1]) ? 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 && !used[x][y - 1]) ? a[x][y - 1] : -1e9); res += ((x + 1 > 0 && y > 0 && x + 1 <= n && y <= m && !used[x + 1][y]) ? a[x + 1][y] : -1e9); res += ((x > 0 && y + 1 > 0 && x <= n && y + 1 <= m && !used[x][y + 1]) ? 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 && !used[x - 1][y]) ? a[x - 1][y] : -1e9); res += ((x + 1 > 0 && y > 0 && x + 1 <= n && y <= m && !used[x + 1][y]) ? a[x + 1][y] : -1e9); res += ((x > 0 && y - 1 > 0 && x <= n && y - 1 <= m && !used[x][y - 1]) ? 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; used.resize(n + 1, vector<ll>(m + 1)); for(auto i: d) used[i.first][i.second] = 1; bool isitpossible = false; for(auto i: d){ if(check(i.first, i.second, a, 1) < 0 && check(i.first, i.second, a, 2) < 0 && check(i.first, i.second, a, 3) < 0 && check(i.first, i.second, a, 4) < 0 ) isitpossible = true; } return 0; if(isitpossible){ cout << "No" << endl; return 0; } set<ll>row; for(auto i: d){ row.insert(i.first); } if(row.size() == 1){ sort(d.begin(), d.end()); vector<vector<ll>>dp(k, vector<ll>(5)); for(int i = 0; i < k; i++) for(int j = 1; j <= 4; j++) dp[i][j] = -1e18; 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 j2 = 1; j2 <= 4; j2++){ if(d[i].second - d[i - 1].second == 1){ if(j == 1 && (j2 == 3 || j2 == 4 || j2 == 1)) continue; if(j == 2 && (j2 == 3 || j2 == 4 || j2 == 1)) continue; if(j == 3 && (j2 == 1 || j2 == 4 || j2 == 3)) continue; } else if(d[i].second - d[i - 1].second == 0){ if(j == 4 && j2 == 2) k = k; else continue; } ll cnt = check(d[i].first, d[i].second, a, j2); if(cnt < 0) cnt = -1e18; dp[i][j2] = max(dp[i][j2], dp[i - 1][j] + cnt); } } } ll mx = -1e18; 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; } else 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 if(k > 10){ 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; } else{ 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; } /* 3 6 1 2 3 2 1 2 3 4 5 1 2 9 6 2 1 3 4 5 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...