#include <bits/stdc++.h>
//ako ovaj vaso daso misli da me pobedjuje.....
using namespace std;
#define int long long
#define f first
#define s second
const int N = 2010;
int n, m, a[N][N], vis[N][N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> m;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cin >> a[i][j];
}
}
int l = 0, r = 1e9, ans = -1;
auto reset = [&]() {
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++)
vis[i][j] = 0;
}
};
auto other = [&]() {
int mn = 1e9, mx = 0;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
if (vis[i][j])
continue;
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
}
return mx - mn;
};
auto check = [&](int mid) {
int mn = 1e9, mx = 0;
reset();
for (int i = 0;i <= m;i++)
vis[0][i] = 1;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
int tmx = max(mx, a[i][j]);
int tmn = min(mn, a[i][j]);
if (tmx - tmn <= mid && vis[i - 1][j]) {
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
vis[i][j] = 1;
}
else
break;
}
}
if (other() <= mid)
return true;
mn = 1e9, mx = 0;
reset();
for (int i = 0;i <= m;i++)
vis[0][i] = 1;
for (int i = 1;i <= n;i++) {
for (int j = m;j >= 1;j--) {
int tmx = max(mx, a[i][j]);
int tmn = min(mn, a[i][j]);
if (tmx - tmn <= mid && vis[i - 1][j]) {
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
vis[i][j] = 1;
}
else
break;
}
}
if (other() <= mid)
return true;
mn = 1e9, mx = 0;
reset();
for (int i = 0;i <= m;i++)
vis[n + 1][i] = 1;
for (int i = n;i >= 1;i--) {
for (int j = 1;j <= m;j++) {
int tmx = max(mx, a[i][j]);
int tmn = min(mn, a[i][j]);
if (tmx - tmn <= mid && vis[i + 1][j]) {
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
vis[i][j] = 1;
}
else
break;
}
}
if (other() <= mid)
return true;
mn = 1e9, mx = 0;
reset();
for (int i = 0;i <= m;i++)
vis[n + 1][i] = 1;
for (int i = n;i >= 1;i--) {
for (int j = m;j >= 1;j--) {
int tmx = max(mx, a[i][j]);
int tmn = min(mn, a[i][j]);
if (tmx - tmn <= mid && vis[i + 1][j]) {
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
vis[i][j] = 1;
}
else
break;
}
}
if (other() <= mid)
return true;
return false;
};
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid)) {
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2512 KB |
Output is correct |
12 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2512 KB |
Output is correct |
12 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2512 KB |
Output is correct |
12 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |