#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 2003;
const int INF = 1000000009;
int n, m;
int a[N][N];
bool stg(int x, int y, int z)
{
int maxu = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
maxu = max(maxu, a[i][j]);
}
if (maxu - a[x][y] <= z)
return true;
int min1 = a[x][y];
int max1 = 0;
int min2 = INF;
int max2 = 0;
int l = y;
int r = m;
for (int i = 1; i <= n; ++i)
{
if (l > r)
return false;
for (int j = 1; j <= l; ++j)
{
max1 = max(max1, a[i][j]);
}
for (int j = l + 1; j <= r; ++j)
{
if (a[i][j] - min1 > z)
{
r = j - 1;
break;
}
max1 = max(max1, a[i][j]);
}
for (int j = r + 1; j <= m; ++j)
{
min2 = min(min2, a[i][j]);
max2 = max(max2, a[i][j]);
}
if (i == x)
{
l = 0;
}
}
return (max1 - min1 <= z && max2 - min2 <= z);
}
int solvv()
{
int mini = 1;
int minj = 1;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (a[i][j] < a[mini][minj])
{
mini = i;
minj = j;
}
}
}
if (mini == n && minj == m)
return INF;
int l = 0, r = INF;
int ans;
while (l <= r)
{
int m = (l + r) / 2;
if (stg(mini, minj, m))
{
ans = m;
r = m - 1;
}
else
l = m + 1;
}
return ans;
}
void frt()
{
for (int i = 1; i <= n; ++i)
{
reverse(a[i] + 1, a[i] + m + 1);
}
}
void frs()
{
for (int j = 1; j <= m; ++j)
{
vector<int> v;
for (int i = 1; i <= n; ++i)
v.push_back(a[i][j]);
reverse(all(v));
for (int i = 1; i <= n; ++i)
a[i][j] = v[i - 1];
}
}
void solv()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
cin >> a[i][j];
}
int ans = solvv();
frt();
ans = min(ans, solvv());
frs();
ans = min(ans, solvv());
frt();
ans = min(ans, solvv());
cout << ans << "\n";
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
Compilation message
joioi.cpp: In function 'int solvv()':
joioi.cpp:85:9: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
85 | int ans;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
476 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
476 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
2536 KB |
Output is correct |
17 |
Correct |
7 ms |
2908 KB |
Output is correct |
18 |
Correct |
6 ms |
2908 KB |
Output is correct |
19 |
Correct |
7 ms |
2908 KB |
Output is correct |
20 |
Correct |
6 ms |
2652 KB |
Output is correct |
21 |
Correct |
8 ms |
2908 KB |
Output is correct |
22 |
Correct |
9 ms |
2792 KB |
Output is correct |
23 |
Correct |
9 ms |
2908 KB |
Output is correct |
24 |
Correct |
7 ms |
2908 KB |
Output is correct |
25 |
Correct |
8 ms |
3000 KB |
Output is correct |
26 |
Correct |
8 ms |
2908 KB |
Output is correct |
27 |
Correct |
11 ms |
3020 KB |
Output is correct |
28 |
Correct |
8 ms |
2996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
476 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
2536 KB |
Output is correct |
17 |
Correct |
7 ms |
2908 KB |
Output is correct |
18 |
Correct |
6 ms |
2908 KB |
Output is correct |
19 |
Correct |
7 ms |
2908 KB |
Output is correct |
20 |
Correct |
6 ms |
2652 KB |
Output is correct |
21 |
Correct |
8 ms |
2908 KB |
Output is correct |
22 |
Correct |
9 ms |
2792 KB |
Output is correct |
23 |
Correct |
9 ms |
2908 KB |
Output is correct |
24 |
Correct |
7 ms |
2908 KB |
Output is correct |
25 |
Correct |
8 ms |
3000 KB |
Output is correct |
26 |
Correct |
8 ms |
2908 KB |
Output is correct |
27 |
Correct |
11 ms |
3020 KB |
Output is correct |
28 |
Correct |
8 ms |
2996 KB |
Output is correct |
29 |
Correct |
580 ms |
37240 KB |
Output is correct |
30 |
Correct |
533 ms |
37612 KB |
Output is correct |
31 |
Correct |
593 ms |
39248 KB |
Output is correct |
32 |
Correct |
572 ms |
39040 KB |
Output is correct |
33 |
Correct |
508 ms |
34864 KB |
Output is correct |
34 |
Correct |
573 ms |
39504 KB |
Output is correct |
35 |
Correct |
847 ms |
54792 KB |
Output is correct |
36 |
Correct |
746 ms |
49912 KB |
Output is correct |
37 |
Correct |
806 ms |
54868 KB |
Output is correct |