# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
752825 | guagua0407 | The Kingdom of JOIOI (JOI17_joioi) | C++17 | 1391 ms | 102004 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#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 check(int mid){
int best=m;
int sir[n];
for(int i=0;i<n;i++){
int j=0;
while(j<best and a[i][j]<=mn+mid) j++;
best=j;
sir[i]=j;
}
int mnn=1e9;
int mxx=0;
for(int i=0;i<n;i++){
for(int j=sir[i];j<m;j++){
mnn=min(mnn,a[i][j]);
mxx=max(mxx,a[i][j]);
}
}
return (mxx-mnn)<=mid;
}
bool ok(int mid){
if(check(mid)) return true;
reverse(a,a+n);
if(check(mid)) return true;
for(int i=0;i<n;i++){
reverse(a[i],a[i]+m);
}
if(check(mid)) return true;
reverse(a,a+n);
if(check(mid)) return true;
return false;
}
signed 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
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |