Submission #386167

#TimeUsernameProblemLanguageResultExecution timeMemory
386167idontreallyknowThe Kingdom of JOIOI (JOI17_joioi)C++17
100 / 100
1000 ms133484 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";}; template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";}; template <class T> void pv(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) cerr << *i << " "; cerr << "]\n";} void _print() {cerr << "]\n";} template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);} #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #define fi first #define se second #define SZ(x) (int)((x).size()) #define pii pair<int,int> const int mx = 2e3+5; int n,m,a[mx][mx], lma[mx][mx], lmi[mx][mx], rma[mx][mx], rmi[mx][mx], small = INT_MAX, large = INT_MIN, ans; void recalc() { for (int q = 0; q < n; q++) { lma[q][0] = a[q][0]; lmi[q][0] = a[q][0]; rma[q][m-1] = a[q][m-1]; rmi[q][m-1] = a[q][m-1]; for (int w = 1; w < m; w++) { lma[q][w] = max(lma[q][w-1], a[q][w]); lmi[q][w] = min(lmi[q][w-1], a[q][w]); } for (int w = m-2; w >= 0; w--) { rma[q][w] = max(rma[q][w+1], a[q][w]); rmi[q][w] = min(rmi[q][w+1], a[q][w]); } } } bool check(int x) { int lasma = 0, lasmi = 0; bool okma = true, okmi = true; for (int q = 0; q < n; q++) { for (int w = lasma; w < m; w++) { if (w < m-1) { if (lma[q][w]-small <= x && large-rmi[q][w+1] <= x) { lasma = w; break; } } else { if (lma[q][w]-small <= x) lasma = w; else okma = false; } } for (int w = lasmi; w < m; w++) { if (w < m-1) { if (large-lmi[q][w] <= x && rma[q][w+1]-small <= x) { lasmi = w; break; } } else { if (large-lmi[q][w] <= x) lasmi = w; else okmi = false; } } } if (okmi || okma) return true; return false; } const int inf = 1e9; void work() { int lo = -1, hi = inf+1; while (hi - lo > 1) { int mid = (lo+hi)/2; if(check(mid)) hi = mid; else lo = mid; } ans = min(ans, hi); } void printa() { cout << "BEGIN\n"; for (int q = 0; q < n; q++) { for (int w = 0; w < m; w++) { cout << a[q][w] << " "; } cout << "\n"; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int q = 0; q < n; q++) { for (int w = 0; w < m; w++) { cin >> a[q][w]; small = min(small, a[q][w]); large = max(large, a[q][w]); } } ans = large-small; recalc(); work(); int tmp[mx][mx]; for (int q = 0; q < n; q++) { for (int w = 0; w < m; w++) { tmp[w][q] = a[q][w]; } } swap(n,m); for (int q = 0; q < n; q++) { for (int w = 0; w < m; w++) { a[q][w] = tmp[q][w]; } } recalc(); work(); for (int q = 0; q < n; q++) { for (int w = 0; w < m; w++) { tmp[m-1-w][q] = a[q][w]; } } swap(n,m); for (int q = 0; q < n; q++) { for (int w = 0; w < m; w++) { a[q][w] = tmp[q][w]; } } recalc(); work(); for (int q = 0; q < n; q++) { for (int w = 0; w < m; w++) { tmp[w][q] = a[q][w]; } } swap(n,m); for (int q = 0; q < n; q++) { for (int w = 0; w < m; w++) { a[q][w] = tmp[q][w]; } } recalc(); work(); cout << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...