#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 , M = N * 64 , MX = 2e4 + 40;
const ll MOD = 1e9 + 7 , oo = 1e9 + 9 , OO = 1e18 , mod = MOD ;
const double pi = acos(-1) , eps = 1e-17 ;
int di[] = {1 , -1 , 0 , 0};
int dj[] = {0 , 0 , -1 , 1};
set<int> st ;
vector<int> v ;
int n , m ;
int arr[N][N];
int sz ;
int MN[N][N] , MXY[N][N] ,RMN[N][N] , RMX[N][N];
bool check(int from , int to){
int mx = 0 , mn = 1e9 , en = n;
//cout << from << " " << to << endl ;
for(int c = 0 ; c < m ; c ++){
int l = 0 , r = n - 1, md ;
int ans = -1 ;
while(l <= r){
md = (l + r) / 2;
//debug(MXY[md][c])
if(MN[md][c] >= from && MXY[md][c] <= to){
ans = md , l = md +1 ;
}
else r = md - 1 ;
}
// debug(ans)
en = min(en , ans + 1);
// debug(en)
if(en != n )mx = max(mx ,RMX[en][c]) , mn= min(mn ,RMN[en][c]);
}
return mx-mn <= to - from;
}
void process(){
for(int j = 0 ;j < m;j ++){
for(int i = 0 ; i < n ; i ++){
MN[i][j]= arr[i][j] , MXY[i][j] = arr[i][j];
if(i)MN[i][j] = min(MN[i][j] , MN[i - 1][j]) , MXY[i][j] = max(MXY[i - 1][j] , MXY[i][j]);
}
}
for(int j = 0 ; j < m ; j ++){
for(int i = n - 1 ; i >= 0 ; i --){
RMN[i][j]= arr[i][j] , RMX[i][j] = arr[i][j];
if(i != n - 1)RMN[i][j] = min(RMN[i][j] , RMN[i + 1][j]) , RMX[i][j] = max(RMX[i + 1][j] , RMX[i][j]);
}
}
}
int arr2[N][N];
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];
arr2[i][j] = arr[i][j];
st.insert(arr[i][j]);
}
}
for(auto f : st)v.push_back(f);
process();
sz = v.size();
ll mnans = v.back() - v[0];
for(int all = 0 ; all < 2 ; all ++){
int l = 0 , r = v.back() - v[0] , md ;
while(l <= r){
md = (l + r) / 2 ;
if(check(v[0] , v[0] + md) || check(v.back() - md , v.back()))mnans = min(mnans , md) , r = md- 1 ;
else l = md + 1 ;
}
for(int c = 0 ; c < m ; c ++){
for(int r = 0 ; r< n ; r ++){
arr[r][c] = arr2[r][m - 1 - c];
// debug(arr[r][c])
}
}
process();
}
cout << mnans ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
2 ms |
716 KB |
Output is correct |
16 |
Correct |
7 ms |
6864 KB |
Output is correct |
17 |
Correct |
20 ms |
9424 KB |
Output is correct |
18 |
Correct |
17 ms |
9432 KB |
Output is correct |
19 |
Correct |
19 ms |
9480 KB |
Output is correct |
20 |
Correct |
23 ms |
8432 KB |
Output is correct |
21 |
Correct |
19 ms |
9480 KB |
Output is correct |
22 |
Correct |
18 ms |
9476 KB |
Output is correct |
23 |
Correct |
19 ms |
9580 KB |
Output is correct |
24 |
Correct |
16 ms |
8548 KB |
Output is correct |
25 |
Correct |
20 ms |
9496 KB |
Output is correct |
26 |
Correct |
19 ms |
9536 KB |
Output is correct |
27 |
Correct |
23 ms |
9540 KB |
Output is correct |
28 |
Correct |
19 ms |
9544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
2 ms |
716 KB |
Output is correct |
16 |
Correct |
7 ms |
6864 KB |
Output is correct |
17 |
Correct |
20 ms |
9424 KB |
Output is correct |
18 |
Correct |
17 ms |
9432 KB |
Output is correct |
19 |
Correct |
19 ms |
9480 KB |
Output is correct |
20 |
Correct |
23 ms |
8432 KB |
Output is correct |
21 |
Correct |
19 ms |
9480 KB |
Output is correct |
22 |
Correct |
18 ms |
9476 KB |
Output is correct |
23 |
Correct |
19 ms |
9580 KB |
Output is correct |
24 |
Correct |
16 ms |
8548 KB |
Output is correct |
25 |
Correct |
20 ms |
9496 KB |
Output is correct |
26 |
Correct |
19 ms |
9536 KB |
Output is correct |
27 |
Correct |
23 ms |
9540 KB |
Output is correct |
28 |
Correct |
19 ms |
9544 KB |
Output is correct |
29 |
Correct |
1906 ms |
185148 KB |
Output is correct |
30 |
Correct |
1871 ms |
194416 KB |
Output is correct |
31 |
Correct |
1840 ms |
194400 KB |
Output is correct |
32 |
Correct |
1886 ms |
194732 KB |
Output is correct |
33 |
Correct |
1531 ms |
170760 KB |
Output is correct |
34 |
Correct |
1907 ms |
194616 KB |
Output is correct |
35 |
Execution timed out |
4070 ms |
217664 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |