Submission #767335

#TimeUsernameProblemLanguageResultExecution timeMemory
767335sysiaThe Kingdom of JOIOI (JOI17_joioi)C++17
100 / 100
607 ms70660 KiB
//Sylwia Sapkowska #include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") using namespace std; void __print(int x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << "'" << x << "'";} void __print(const char *x) {cerr << '"' << x << '"';} void __print(const string &x) {cerr << '"' << x << '"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifdef LOCAL #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define int long long typedef pair<int, int> T; const int oo = 1e18, oo2 = 1e9+7, K = 30; const int mod = 998244353; void solve(){ int n, m; cin >> n >> m; vector a(n+1, vector<int>(m+1)); int mx = -oo, mn = oo; T A, B; for (int i = 1; i<=n; i++){ for (int j = 1; j<=m; j++){ cin >> a[i][j]; if (a[i][j] > mx){ mx = a[i][j]; A = {i, j}; } if (a[i][j] < mn){ mn = a[i][j]; B = {i, j}; } } } if (mn == mx){ cout << "0\n"; return; } int ans = mx-mn; auto sortuj = [&](vector<int>L, vector<int>R){ debug(L, R); //a[i] \in [L[i], R[i]] //1) increasing bool ok = 1; vector<int>tab(n+1); for (int i = 1; i<=n; i++){ tab[i] = L[i]; if (i > 1) tab[i] = max(tab[i], tab[i-1]); if (tab[i] > R[i] || tab[i] < L[i]){ ok = 0; break; } } if (ok) return true; ok = 1; //2) decreasing for (int i = 1; i<=n; i++){ tab[i] = R[i]; if (i > 1) tab[i] = min(tab[i], tab[i-1]); if (tab[i] > R[i] || tab[i] < L[i]){ ok = 0; break; } } return ok; }; auto check = [&](int k){ //max(A-mn, mx-B) = k; //A <= k + mn //B >= mx - k debug(k+mn, mx-k); //sprawdzic czy da sie przypisac //0) z lewa minimum //1) z lewa maksimum for (int rep = 0; rep < 2; rep++){ vector<int>R(n+1, m), L(n+1, 0); bool ok = 1; for (int i = 1; i<=n; i++){ for (int j = 1; j<=m; j++){ if ((!rep && a[i][j] > k + mn) || (rep && a[i][j] < mx - k)){ R[i] = j-1; break; } } for (int j = R[i]+1; j<=m; j++){ if (!rep) ok &= (a[i][j] >= mx-k); else ok &= (a[i][j] <= k+mn); } for (int j = R[i]; j>=1; j--){ if ((!rep && a[i][j] < mx-k) || (rep && a[i][j] > k+mn)){ L[i] = j; break; } } } if (ok && sortuj(L, R)) return true; } return false; }; //ans = max(mx-A, B-mn); int l = 1, r = oo; while (r >= l){ int mid = (l+r)/2; if (check(mid)){ ans = mid; r = mid-1; } else { l = mid+1; } } ans = min(ans, mx-mn); cout << ans << "\n"; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; //cin >> t; while (t--) solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...