Submission #996289

#TimeUsernameProblemLanguageResultExecution timeMemory
996289yanbThe Kingdom of JOIOI (JOI17_joioi)C++14
100 / 100
1548 ms101840 KiB
#include <bits/stdc++.h> using namespace std; #define int long long int mn = 1e11, mx = -1e11; bool good(int n, int m, vector<vector<int>> &a, int x) { int pos = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] <= mx - x) pos = max(j, pos); } for (int j = 0; j < m; j++) { if (a[i][j] >= mn + x && j <= pos) return 0; } } return 1; } int solve(int n, int m, vector<vector<int>> &a) { /*for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << a[i][j] << " "; } cout << "\n"; }*/ int l = -1, r = 2e9; while (r - l > 1) { int md = (l + r) / 2; if (good(n, m, a, md)) { //cout << "Good " << md << "\n"; r = md; } else { //cout << "Bad " << md << "\n"; l = md; } } //cout << l << "\n\n"; //cout << "." << endl; return l; } vector<vector<int>> flipv(int n, int m, vector<vector<int>> &a) { vector<vector<int>> ans(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[i][j] = a[i][m - j - 1]; } } return ans; } vector<vector<int>> flips(int n, int m, vector<vector<int>> &a) { mn = -mn, mx = -mx; swap(mn, mx); vector<vector<int>> ans(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[i][j] = -a[i][j]; } } return ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<int>> a(n, vector<int>(m)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { mn = min(mn, a[i][j]); mx = max(mx, a[i][j]); } } int ans = solve(n, m, a); reverse(a.begin(), a.end()); ans = min(ans, solve(n, m, a)); a = flipv(n, m, a); ans = min(ans, solve(n, m, a)); reverse(a.begin(), a.end()); ans = min(ans, solve(n, m, a)); a = flips(n, m, a); ans = min(ans, solve(n, m, a)); reverse(a.begin(), a.end()); ans = min(ans, solve(n, m, a)); a = flipv(n, m, a); ans = min(ans, solve(n, m, a)); reverse(a.begin(), a.end()); ans = min(ans, solve(n, m, a)); cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...