# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
480717 | Mackerel_Pike | The Kingdom of JOIOI (JOI17_joioi) | C++14 | 628 ms | 54936 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.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005;
int n, m, mn, mx;
int a[maxn][maxn];
int l[maxn], r[maxn];
inline bool solve(int md){
for(int i = 0; i < n; ++i){
for(r[i] = -1; r[i] + 1 < m && a[i][r[i] + 1] >= mx - md; ++r[i]);
for(l[i] = m; l[i] - 1 >= 0 && a[i][l[i] - 1] <= mn + md; --l[i]);
--l[i];
if(l[i] > r[i])
return false;
}
int lst = -1; bool ok = true;
for(int i = 0; i < n; ++i){
lst = max(lst, l[i]);
ok &= (lst <= r[i]);
}
if(ok)
return true;
lst = m;
for(int i = 0; i < n; ++i){
lst = min(lst, r[i]);
if(lst < l[i])
return false;
}
return true;
}
inline bool check(int md){
if(solve(md))
return true;
for(int i = 0; i < n; ++i)
reverse(a[i], a[i] + m);
return solve(md);
}
int main(){
//freopen("joioi.in", "r", stdin);
scanf("%d%d", &n, &m);
mx = 0, mn = 0x3f3f3f3f;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
scanf("%d", &a[i][j]), mx = max(mx, a[i][j]), mn = min(mn, a[i][j]);
int lb = -1, rb = 1e9;
for(; lb + 1 < rb; ){
int md = lb + rb >> 1;
if(check(md))
rb = md;
else
lb = md;
}
printf("%d\n", rb);
return 0;
}
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... |