This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
#include <climits>
using namespace std;
using ll = long long;
int h, w;
int a[2005][2005];
int minv, maxv;
int t[2005][2005];
int t2[2005][2005];
int t3[2005][2005];
int t4[2005][2005];
// int t1[3][2005][2005];
// int t2[3][2005][2005];
// int t3[3][2005][2005];
// int t4[3][2005][2005];
bool check(int thr) {
// cout << "!!" << thr << endl;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
t[i][j] = 0;
if (a[i][j] <= minv + thr) {
t[i][j] += 1;
}
if (a[i][j] >= maxv - thr) {
t[i][j] += 2;
}
// cout << i << " " << j << " " << t[i][j] << endl;
if (t[i][j] == 0) return false;
t2[w - j + 1][i] = t[i][j];
t3[h - i + 1][w - j + 1] = t[i][j];
t4[j][h - i + 1] = t[i][j];
}
}
bool flag1 = 1, flag2 = 1, flag3 = 1, flag4 = 1;
{
int small_right_most = 0;
int big_left_most = w + 1;
for (int i = 1; i <= h && flag1 == 1; i++) {
int cur_small_right_most = 0;
int cur_big_left_most = w + 1;
for (int j = 1; j <= w; j++) {
if (t[i][j] == 1) {
cur_small_right_most = max(cur_small_right_most, j);
}
if (t[i][j] == 2) {
cur_big_left_most = min(cur_big_left_most, j);
}
}
if (cur_small_right_most >= cur_big_left_most) {
flag1 = 0;
break;
}
if (small_right_most >= cur_big_left_most) {
flag1 = 0;
break;
}
small_right_most = max(small_right_most, cur_small_right_most);
big_left_most = cur_big_left_most;
}
}
{
int small_right_most = 0;
int big_left_most = w + 1;
for (int i = 1; i <= h && flag3 == 1; i++) {
int cur_small_right_most = 0;
int cur_big_left_most = w + 1;
for (int j = 1; j <= w; j++) {
if (t3[i][j] == 1) {
cur_small_right_most = max(cur_small_right_most, j);
}
if (t3[i][j] == 2) {
cur_big_left_most = min(cur_big_left_most, j);
}
}
if (cur_small_right_most >= cur_big_left_most) {
flag3 = 0;
break;
}
if (small_right_most >= cur_big_left_most) {
flag3 = 0;
break;
}
small_right_most = max(small_right_most, cur_small_right_most);
big_left_most = cur_big_left_most;
}
}
{
int small_right_most = 0;
int big_left_most = h + 1;
for (int i = 1; i <= w && flag2 == 1; i++) {
int cur_small_right_most = 0;
int cur_big_left_most = h + 1;
for (int j = 1; j <= h; j++) {
if (t2[i][j] == 1) {
cur_small_right_most = max(cur_small_right_most, j);
}
if (t2[i][j] == 2) {
cur_big_left_most = min(cur_big_left_most, j);
}
}
if (cur_small_right_most >= cur_big_left_most) {
flag2 = 0;
break;
}
if (small_right_most >= cur_big_left_most) {
flag2 = 0;
break;
}
small_right_most = max(small_right_most, cur_small_right_most);
big_left_most = cur_big_left_most;
}
}
{
int small_right_most = 0;
int big_left_most = h + 1;
for (int i = 1; i <= w && flag4 == 1; i++) {
int cur_small_right_most = 0;
int cur_big_left_most = h + 1;
for (int j = 1; j <= h; j++) {
if (t4[i][j] == 1) {
cur_small_right_most = max(cur_small_right_most, j);
}
if (t4[i][j] == 2) {
cur_big_left_most = min(cur_big_left_most, j);
}
}
if (cur_small_right_most >= cur_big_left_most) {
flag4 = 0;
break;
}
if (small_right_most >= cur_big_left_most) {
flag4 = 0;
break;
}
small_right_most = max(small_right_most, cur_small_right_most);
big_left_most = cur_big_left_most;
}
}
return flag1 || flag2 || flag3 || flag4;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> h >> w;
minv = INT_MAX;
maxv = INT_MIN;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> a[i][j];
minv = min(minv, a[i][j]);
maxv = max(maxv, a[i][j]);
}
}
if (minv == maxv) {
cout << 0 << endl;
return 0;
}
int l = 0, r = maxv - minv;
while (l <= r) {
int d = (l + r) / 2;
if (check(d)) {
r = d - 1;
}
else {
l = d + 1;
}
}
cout << l << endl;
}
Compilation message (stderr)
joioi.cpp: In function 'bool check(int)':
joioi.cpp:42:7: warning: variable 'big_left_most' set but not used [-Wunused-but-set-variable]
42 | int big_left_most = w + 1;
| ^~~~~~~~~~~~~
joioi.cpp:68:7: warning: variable 'big_left_most' set but not used [-Wunused-but-set-variable]
68 | int big_left_most = w + 1;
| ^~~~~~~~~~~~~
joioi.cpp:94:7: warning: variable 'big_left_most' set but not used [-Wunused-but-set-variable]
94 | int big_left_most = h + 1;
| ^~~~~~~~~~~~~
joioi.cpp:120:7: warning: variable 'big_left_most' set but not used [-Wunused-but-set-variable]
120 | int big_left_most = h + 1;
| ^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |