#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout << '[' << #x << " is: " << x << "] " << endl;
#define inF freopen("6in.txt" , "r" , stdin);
#define outF freopen("6.txt" , "w" , stdout);
#define imod(a , n) (a % n + n ) % n
#define sor(v) sort(v.begin() , v.end());
#define print(v) for(auto f : v ) cout << f << " " ;
#define rsor(v) sort(v.rbegin() , v.rend());
#define rev(v) reverse(v.begin() , v.end());
#define scan(v)for(auto &it : v)cin >> it ;
#define ll long long
#define int ll
#define logar(x , y) log(x) / log(y)
#define __sum(n) n * (n + 1) / 2
#define __lcm(a , b) a / __gcd(a , b) * b
#define pii pair<int , int >
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
const int N = 2e3 + 5 ;
int n , m ;
int arr[N][N] , arr2[N][N];
int themax[N][N] , themin[N][N] , rmax[N][N] , rmin[N][N];
void process(){
for(int c = 0 ; c <m ; c ++){
for(int r = 0 ; r < n ; r ++){
themax[r][c] = themin[r][c] = arr[r][c];
if(r)themax[r][c] = max(themax[r][c] , themax[r - 1][c]) , themin[r][c] = min(themin[r][c] , themin[r - 1][c]);
}
}
for(int c = 0 ; c < m ; c ++){
rmin[n][c] = 1e9 , rmax[n][c] = 0 ;
for(int r = n - 1 ; r >= 0 ; r --){
rmin[r][c] = rmax[r][c] = arr[r][c] ;
rmin[r][c] = min(rmin[r][c] , rmin[r + 1][c]) , rmax[r][c] = max(rmax[r][c] , rmax[r + 1][c]);
}
}
}
int mnmm = 1e9 ;
bool check(int number){
int mx = 0 , mn = 1e9 ;
for(int c = 0 ; c < m; c ++){
int l = 0 , r = n - 1 , md , ans = n;
int en = n ;
while(l <= r ){
md = (l + r) / 2 ;
if(themax[md][c] > number)
ans = md , r = md - 1 ;
else l = md + 1 ;
}
en = min(en , ans);
mx = max(mx , rmax[en][c] ), mn = min(mn , rmin[en][c]);
}
return mx - mn <= number - mnmm ;
}
int32_t main()
{ //inF;
//inF;outF;
fastio;
cin >> n >>m;
for(int i = 0 ;i < n ; i ++)
for(int j = 0 ; j < m ; j ++){
cin >> arr[i][j];
mnmm = min(mnmm , arr[i][j]);
arr2[i][j] = arr[i][j] ;
}
int mnans = 1e9 ;
for(int c = 0 ; c < 4 ; c ++){
process();
int l = 0 , r = 1e9 , md ;
while(l <= r ){
md = (l + r) / 2 ;
if(check(md + mnmm))mnans = min(mnans , md) , r = md - 1 ;
else l = md + 1;
}
int row = 0 , col = m - 1 ;
for(int j = 0 ; row <n; j ++){
for(int r = 0 ; r < n ; r ++){
arr[r][j] = arr2[row][col--];
if(col < 0)break;
}
row ++ ;
col = m - 1 ;
}
swap(n , m);
for(int i = 0 ; i < n ; i ++){
for(int j = 0 ; j < m ; j ++){
//cout << arr[i][j] << " " ;
arr2[i][j] = arr[i][j];
}
// cout << endl ;
}
// debug(mnans)
}
cout << mnans ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |