# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
751623 | 2023-06-01T02:28:45 Z | guagua0407 | The Kingdom of JOIOI (JOI17_joioi) | C++17 | 0 ms | 0 KB |
//#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define ll long long #define int ll #define pii pair<int,int> #define f first #define s second #define all(x) x.begin(),x.end() #define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } const int mxn=2e3+5; int a[mxn][mxn]; vector<int> vec; int n,m; int mn=1e9; int mx=0; bool ok(int mid){ int l=upper_bound(all(vec),mn+mid)-vec.begin()-1; int r=lower_bound(all(vec),mx-mid)-vec.begin(); if(l<r-1) return false; vector<vector<int>> vis(n,vector<int>(m,0)); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(a[i][j]<=min(mn+mid,mx-mid-1)){ vis[i][j]=-1; } else if(a[i][j]>=max(mn+mid+1,mx-mid)){ vis[i][j]=1; } } } for(int i=0;i<n;i++){ int cur=0; int a=0; int b=0; for(int j=0;j<m;j++){ if(vis[i][j]==-1){ if(cur==1){ if(a>0) return false; else{ cur=-1; a++; } } else{ cur=-1; a++; } } else if(vis[i][j]==1){ if(cur==-1){ if(b>0) return false; else{ cur=1; b++; } } else{ cur=1; b++; } } } } for(int i=0;i<m;i++){ int cur=0; int a=0; int b=0; for(int j=0;j<n;j++){ if(vis[j][i]==-1){ if(cur==1){ if(a>0) return false; else{ cur=-1; a++; } } else{ a++; } } else if(vis[j][i]==1){ if(cur==-1){ if(b>0) return false; else{ cur=1; b++; } } else{ b++; } } } } return true; } int main() {_ cin>>n>>m; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>a[i][j]; vec.push_back(a[i][j]); mx=max(mx,a[i][j]); mn=min(mn,a[i][j]); } } sort(all(vec)); int l=0,r=mx-mn; while(l<r){ int mid=(l+r)/2; if(ok(mid)){ r=mid; } else{ l=mid+1; } } cout<<l<<'\n'; return 0; } //maybe its multiset not set