#include <bits/stdc++.h>
using namespace std;
const int mxn = 2022;
int arr[mxn][mxn],lim[mxn];
int n,m;
int small = 1e9+10;
bool calc(int d){
for(lim[1] = 1;lim[1]<=m&&arr[1][lim[1]]<=small+d;lim[1]++);
for(int i = 2;i<=n;i++){
for(lim[i] = 1;lim[i]<lim[i-1]&&arr[i][lim[i]]<=small+d;lim[i]++);
}
int big = 0,small = 1e9;
for(int i = 1;i<=n;i++){
for(int j = m;j>=lim[i];j--)big = max(big,arr[i][j]),small = min(small,arr[i][j]);
}
return big-small<=d;
}
bool f(int d){
bool flag = false;
flag = calc(d);
reverse(arr+1,arr+n+1);
flag = flag||calc(d);
return flag;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m;
int big = 0;
for(int i = 1;i<=n;i++)
for(int j = 1;j<=m;j++)cin>>arr[i][j],big = max(big,arr[i][j]),small = min(small,arr[i][j]);
int l = 0,r = big;
while(l != r){
int mid = (l+r)>>1;
if(f(mid))r = mid;
else l = mid+1;
}
cout<<l;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |