#include <bits/stdc++.h>
using namespace std;
int main()
{
int N, M;
cin >> N >> M;
int high = 0, low = 1000000000;
vector<vector<int>> Tab(N, vector<int>(M));
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
cin >> Tab[i][j];
high = max(high, Tab[i][j]);
low = min(low, Tab[i][j]);
}
}
vector<vector<int>> colmin(N, vector<int>(M)), colmax(N, vector<int>(M)), rowminlr(N, vector<int>(M)), rowminrl(N, vector<int>(M)), rowmaxlr(N, vector<int>(M)), rowmaxrl(N, vector<int>(M));
for (int i = 0; i < M; i++)
{
colmin[N - 1][i] = Tab[N - 1][i];
colmax[N - 1][i] = Tab[N - 1][i];
}
for (int i = N - 2; i >= 0; i--)
{
for (int j = 0; j < M; j++)
{
colmin[i][j] = min(colmin[i + 1][j], Tab[i][j]);
colmax[i][j] = max(colmax[i + 1][j], Tab[i][j]);
}
}
for (int i = 0; i < N; i++)
{
rowminlr[i][0] = Tab[i][0];
rowmaxlr[i][0] = Tab[i][0];
rowminrl[i][M - 1] = Tab[i][M - 1];
rowmaxrl[i][M - 1] = Tab[i][M - 1];
}
for (int j = 1; j < M; j++)
{
for (int i = 0; i < N; i++)
{
rowminlr[i][j] = min(rowminlr[i][j - 1], Tab[i][j]);
rowmaxlr[i][j] = max(rowmaxlr[i][j - 1], Tab[i][j]);
}
}
for (int j = M - 2; j >= 0; j--)
{
for (int i = 0; i < N; i++)
{
rowminrl[i][j] = min(rowminrl[i][j + 1], Tab[i][j]);
rowmaxrl[i][j] = max(rowmaxrl[i][j + 1], Tab[i][j]);
}
}
int best = 1234567890;
int maxx = 0, minn = 1000000000;
int x = 0, y = M - 1;
while (x < N && y >= 0)
{
int a = min(minn, rowminlr[x][y]);
int b = max(maxx, colmax[x][y]);
if (high - a > b - low)
{
maxx = b;
y--;
}
else
{
minn = a;
x++;
}
}
best = min(best, max(high - minn, maxx - low));
maxx = 0, minn = 1000000000;
x = 0, y = M - 1;
while (x < N && y >= 0)
{
int a = min(minn, colmin[x][y]);
int b = max(maxx, rowmaxlr[x][y]);
if (high - a > b - low)
{
maxx = b;
x++;
}
else
{
minn = a;
y--;
}
}
best = min(best, max(high - minn, maxx - low));
maxx = 0, minn = 1000000000;
x = 0, y = 0;
while (x < N && y < M)
{
int a = min(minn, rowminrl[x][y]);
int b = max(maxx, colmax[x][y]);
if (high - a > b - low)
{
maxx = b;
y++;
}
else
{
minn = a;
x++;
}
}
best = min(best, max(high - minn, maxx - low));
maxx = 0, minn = 1000000000;
x = 0, y = 0;
while (x < N && y < M)
{
int a = min(minn, colmin[x][y]);
int b = max(maxx, rowmaxrl[x][y]);
if (high - a > b - low)
{
maxx = b;
x++;
}
else
{
minn = a;
y++;
}
}
best = min(best, max(high - minn, maxx - low));
cout << best;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
1372 KB |
Output is correct |
17 |
Correct |
10 ms |
1604 KB |
Output is correct |
18 |
Correct |
9 ms |
1628 KB |
Output is correct |
19 |
Correct |
10 ms |
1628 KB |
Output is correct |
20 |
Correct |
8 ms |
1616 KB |
Output is correct |
21 |
Correct |
15 ms |
1880 KB |
Output is correct |
22 |
Correct |
12 ms |
1884 KB |
Output is correct |
23 |
Correct |
12 ms |
1864 KB |
Output is correct |
24 |
Correct |
11 ms |
1624 KB |
Output is correct |
25 |
Correct |
12 ms |
1884 KB |
Output is correct |
26 |
Correct |
15 ms |
1884 KB |
Output is correct |
27 |
Correct |
12 ms |
1860 KB |
Output is correct |
28 |
Correct |
12 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
1372 KB |
Output is correct |
17 |
Correct |
10 ms |
1604 KB |
Output is correct |
18 |
Correct |
9 ms |
1628 KB |
Output is correct |
19 |
Correct |
10 ms |
1628 KB |
Output is correct |
20 |
Correct |
8 ms |
1616 KB |
Output is correct |
21 |
Correct |
15 ms |
1880 KB |
Output is correct |
22 |
Correct |
12 ms |
1884 KB |
Output is correct |
23 |
Correct |
12 ms |
1864 KB |
Output is correct |
24 |
Correct |
11 ms |
1624 KB |
Output is correct |
25 |
Correct |
12 ms |
1884 KB |
Output is correct |
26 |
Correct |
15 ms |
1884 KB |
Output is correct |
27 |
Correct |
12 ms |
1860 KB |
Output is correct |
28 |
Correct |
12 ms |
1880 KB |
Output is correct |
29 |
Correct |
944 ms |
127324 KB |
Output is correct |
30 |
Correct |
991 ms |
123932 KB |
Output is correct |
31 |
Correct |
1008 ms |
133392 KB |
Output is correct |
32 |
Correct |
1004 ms |
133712 KB |
Output is correct |
33 |
Correct |
879 ms |
116308 KB |
Output is correct |
34 |
Correct |
1062 ms |
133820 KB |
Output is correct |
35 |
Correct |
1384 ms |
149288 KB |
Output is correct |
36 |
Correct |
1157 ms |
130320 KB |
Output is correct |
37 |
Correct |
1403 ms |
149328 KB |
Output is correct |