Submission #1005668

#TimeUsernameProblemLanguageResultExecution timeMemory
1005668dimashhhThe Kingdom of JOIOI (JOI17_joioi)C++17
100 / 100
1062 ms70996 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e3 + 2, MOD = (int)1e9 + 7; int n,m,a[N][N],b[N][N]; void rot(){ for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ a[m - j + 1][i] = b[i][j]; } } swap(n,m); for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ b[i][j] = a[i][j]; } } } int mn = 1e9; int it[N]; bool check(int mid){ int l = mn,r = l + mid; it[0] = m; for(int i = 1;i <= n;i++){ it[i] = 0; for(int j = 1;j <= it[i - 1] && a[i][j] >= l && a[i][j] <= r;j++){ it[i] = j; } } int mn = 1e9,mx = -1e9; for(int i = 1;i <= n;i++){ for(int j = it[i] + 1;j <= m;j++){ mn = min(mn,a[i][j]); mx = max(mx,a[i][j]); } } return (mx - mn <= mid); } bool ok(int mid){ bool res = false; for(int i = 0;i < 4;i++){ if(check(mid)){ res = 1; } rot(); } return res; } void test(){ cin >> n >> m; for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;++j){ cin >> a[i][j]; b[i][j] = a[i][j]; mn = min(mn,a[i][j]); } } int l = -1,r = 1e9 + 1; while(r - l > 1){ int mid = (l + r) >> 1; if(ok(mid)){ r = mid; }else{ l = mid; } // cout << mid << ' ' << ok(mid) << ' ' << check(mid) << '\n'; } cout << r << '\n'; } int main() { ios_base::sync_with_stdio(false);cin.tie(0); int t = 1; // cin >> t; while(t--){ test(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...