#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
#define int long long
#define nl '\n'
#define io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
const int mod = 1000000007, mod2 = 998244353;
// change this
const int N = 2005;
int n, m, arr[N][N], k, mini = INT_MAX, maxi = 0;
bool can() {
// confirm leftmost corner
for (int i=0; i<n; i++) {
// find first place where element > mini+k
int fp = n;
for (int j=0; j<m; j++) {
if (arr[i][j] > mini + k) {
fp = j;
break;
}
}
// find last place where element < maxi-k
int lp = -1;
for (int j=m-1; j>=0; j--) {
if (arr[i][j] < maxi - k) {
lp = j;
break;
}
}
if (fp < lp) return false;
}
return true;
}
void flipr() {
for (int i=0; i<n; i++) reverse(arr[i], arr[i]+m);
}
void flipc() {
for (int i=0; i<m; i++) {
for (int j=0; j<n/2; j++) swap(arr[j][i], arr[n-j-1][i]);
}
}
int verify(int kk) {
k = kk;
if (can()) return true;
flipr();
if (can()) return true;
flipc();
if (can()) return true;
flipr();
if (can()) return true;
return false;
}
signed main() {
io;
cin >> n >> m;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
cin >> arr[i][j];
mini = min(mini, arr[i][j]);
maxi = max(maxi, arr[i][j]);
}
}
int l = 0, r = 1e9+5;
while (l<r) {
int mid = (l+r)/2;
if (verify(mid)) r = mid;
else l = mid+1;
}
cout << l << nl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |