#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m, A[2005][2005], P[2005][2005], S[2005][2005], P2[2005][2005], S2[2005][2005];
void solve(){
cin >> n >> m;
int mn = 1e18, mx = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)cin >> A[i][j], mn = min(mn, A[i][j]), mx = max(mx, A[i][j]);
}
for(int i=1;i<=m;i++){
P2[0][i] = S2[n + 1][i] = 1e18;
for(int j=1;j<=n;j++){
P[j][i] = max(P[j-1][i], A[j][i]);
P2[j][i] = min(P2[j-1][i], A[j][i]);
}
for(int j=n;j>=1;j--){
S[j][i] = max(S[j+1][i], A[j][i]);
S2[j][i] = min(S2[j+1][i], A[j][i]);
}
}
int lo = 0, hi = (int)1e9, ans = hi;
while(lo <= hi){
int mid = (lo + hi) >> 1;
int ptr = n;
for(int i=1;i<=m;i++){
while(ptr >= 1 && (P[ptr][i] > mn + mid || S2[ptr + 1][i] < mx - mid))ptr--;
}
bool f = (ptr >= 1);
ptr = n;
for(int i=m;i>=1;i--){
while(ptr >= 1 && (P[ptr][i] > mn + mid || S2[ptr + 1][i] < mx - mid))ptr--;
}
f |= (ptr >= 1);
ptr = n;
for(int i=1;i<=m;i++){
while(ptr >= 1 && (P2[ptr][i] < mx - mid || S[ptr + 1][i] > mn + mid))ptr--;
}
f |= (ptr >= 1);
ptr = n;
for(int i=m;i>=1;i--){
while(ptr >= 1 && (P2[ptr][i] < mx - mid || S[ptr + 1][i] > mn + mid))ptr--;
}
f |= (ptr >= 1);
if(f)ans = mid, hi = mid - 1;
else lo = mid + 1;
}
cout << ans;
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
joioi.cpp:62:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
62 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4600 KB |
Output is correct |
5 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4600 KB |
Output is correct |
5 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4600 KB |
Output is correct |
5 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |