Submission #908794

#TimeUsernameProblemLanguageResultExecution timeMemory
908794OAleksaThe Kingdom of JOIOI (JOI17_joioi)C++14
0 / 100
2 ms2512 KiB
#include <bits/stdc++.h> //ako ovaj vaso daso misli da me pobedjuje..... using namespace std; #define int long long #define f first #define s second const int N = 2010; int n, m, a[N][N], vis[N][N]; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tt = 1; //cin >> tt; while (tt--) { cin >> n >> m; for (int i = 1;i <= n;i++) { for (int j = 1;j <= m;j++) { cin >> a[i][j]; } } int l = 0, r = 1e9, ans = -1; auto reset = [&]() { for (int i = 1;i <= n;i++) { for (int j = 1;j <= m;j++) vis[i][j] = 0; } }; auto other = [&]() { int mn = 1e9, mx = 0; for (int i = 1;i <= n;i++) { for (int j = 1;j <= m;j++) { if (vis[i][j]) continue; mx = max(mx, a[i][j]); mn = min(mn, a[i][j]); } } return mx - mn; }; auto check = [&](int mid) { int mn = 1e9, mx = 0; reset(); for (int i = 0;i <= m;i++) vis[0][i] = 1; for (int i = 1;i <= n;i++) { for (int j = 1;j <= m;j++) { int tmx = max(mx, a[i][j]); int tmn = min(mn, a[i][j]); if (tmx - tmn <= mid && vis[i - 1][j]) { mx = max(mx, a[i][j]); mn = min(mn, a[i][j]); vis[i][j] = 1; } else break; } } if (other() <= mid) return true; mn = 1e9, mx = 0; reset(); for (int i = 0;i <= m;i++) vis[0][i] = 1; for (int i = 1;i <= n;i++) { for (int j = m;j >= 1;j--) { int tmx = max(mx, a[i][j]); int tmn = min(mn, a[i][j]); if (tmx - tmn <= mid && vis[i - 1][j]) { mx = max(mx, a[i][j]); mn = min(mn, a[i][j]); vis[i][j] = 1; } else break; } } if (other() <= mid) return true; mn = 1e9, mx = 0; reset(); for (int i = 0;i <= m;i++) vis[n + 1][i] = 1; for (int i = n;i >= 1;i--) { for (int j = 1;j <= m;j++) { int tmx = max(mx, a[i][j]); int tmn = min(mn, a[i][j]); if (tmx - tmn <= mid && vis[i + 1][j]) { mx = max(mx, a[i][j]); mn = min(mn, a[i][j]); vis[i][j] = 1; } else break; } } if (other() <= mid) return true; mn = 1e9, mx = 0; reset(); for (int i = 0;i <= m;i++) vis[n + 1][i] = 1; for (int i = n;i >= 1;i--) { for (int j = m;j >= 1;j--) { int tmx = max(mx, a[i][j]); int tmn = min(mn, a[i][j]); if (tmx - tmn <= mid && vis[i + 1][j]) { mx = max(mx, a[i][j]); mn = min(mn, a[i][j]); vis[i][j] = 1; } else break; } } if (other() <= mid) return true; return false; }; while (l <= r) { int mid = (l + r) / 2; if (check(mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...