#include <bits/stdc++.h>
using namespace std;
int A[2001][2001], B[2001][2001], n, m;
void Rotate()
{
for (int i = 0; i < n; i++)
{
for (int y = 0; y < m; y++)
{
B[m-y-1][i] = A[i][y];
}
}
for (int i = 0; i <= 2000; i++)
{
for (int y = 0; y <= 2000; y++)
{
A[i][y] = B[i][y];
}
}
swap(n, m);
}
long long Solve()
{
vector <pair<int,pair<int,int>>> Q;
int G[n][m], Max=0, Min=2000000000, Re=2000000000, I, Y, a, Mi2=2000000000, Ma2=0;
for (int i = 0; i < n; i++)
{
for (int y = 0; y < m; y++)
{
Q.push_back({A[i][y],{i,y}});
G[i][y] = 1;
Max = max(A[i][y], Max);
Min = min(A[i][y], Min);
}
}
sort(Q.begin(),Q.end());
Re = Max - Min;
Mi2 = Min;
for (int u = 0; u < n*m; u++)
{
I = Q[u].second.first;
Y = Q[u].second.second;
a = Q[u].first;
if (G[I][Y] == 0)
{
continue;
}
if (Ma2 != 0)
{
Re = min(Re, max(Ma2-Mi2, Max-a));
}
for (int i = I; i >= 0; i--)
{
if (G[i][Y] == 0)
{
break;
}
for (int y = Y; y >= 0; y--)
{
if (G[i][y] == 0)
{
break;
}
Ma2 = max(Ma2, A[i][y]);
G[i][y] = 0;
}
}
}
return Re;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
long long Mi=2000000000;
for (int i = 0; i < n; i++)
{
for (int y = 0; y < m; y++)
{
cin >> A[i][y];
}
}
Mi = min(Mi, Solve());
Rotate();
Mi = min(Mi, Solve());
Rotate();
Mi = min(Mi, Solve());
Rotate();
Mi = min(Mi, Solve());
cout << Mi;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
16000 KB |
Output is correct |
2 |
Correct |
17 ms |
16000 KB |
Output is correct |
3 |
Correct |
19 ms |
16128 KB |
Output is correct |
4 |
Correct |
20 ms |
16256 KB |
Output is correct |
5 |
Correct |
19 ms |
16128 KB |
Output is correct |
6 |
Correct |
17 ms |
16128 KB |
Output is correct |
7 |
Correct |
17 ms |
16128 KB |
Output is correct |
8 |
Correct |
18 ms |
16128 KB |
Output is correct |
9 |
Correct |
27 ms |
16128 KB |
Output is correct |
10 |
Correct |
20 ms |
16128 KB |
Output is correct |
11 |
Correct |
18 ms |
16128 KB |
Output is correct |
12 |
Correct |
19 ms |
16128 KB |
Output is correct |
13 |
Correct |
19 ms |
16128 KB |
Output is correct |
14 |
Correct |
18 ms |
16128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
16000 KB |
Output is correct |
2 |
Correct |
17 ms |
16000 KB |
Output is correct |
3 |
Correct |
19 ms |
16128 KB |
Output is correct |
4 |
Correct |
20 ms |
16256 KB |
Output is correct |
5 |
Correct |
19 ms |
16128 KB |
Output is correct |
6 |
Correct |
17 ms |
16128 KB |
Output is correct |
7 |
Correct |
17 ms |
16128 KB |
Output is correct |
8 |
Correct |
18 ms |
16128 KB |
Output is correct |
9 |
Correct |
27 ms |
16128 KB |
Output is correct |
10 |
Correct |
20 ms |
16128 KB |
Output is correct |
11 |
Correct |
18 ms |
16128 KB |
Output is correct |
12 |
Correct |
19 ms |
16128 KB |
Output is correct |
13 |
Correct |
19 ms |
16128 KB |
Output is correct |
14 |
Correct |
18 ms |
16128 KB |
Output is correct |
15 |
Correct |
22 ms |
16896 KB |
Output is correct |
16 |
Correct |
44 ms |
18376 KB |
Output is correct |
17 |
Correct |
40 ms |
18456 KB |
Output is correct |
18 |
Correct |
48 ms |
18528 KB |
Output is correct |
19 |
Correct |
39 ms |
18464 KB |
Output is correct |
20 |
Correct |
43 ms |
18356 KB |
Output is correct |
21 |
Correct |
44 ms |
18456 KB |
Output is correct |
22 |
Correct |
41 ms |
18420 KB |
Output is correct |
23 |
Correct |
46 ms |
18464 KB |
Output is correct |
24 |
Correct |
38 ms |
18348 KB |
Output is correct |
25 |
Correct |
43 ms |
18500 KB |
Output is correct |
26 |
Correct |
46 ms |
18416 KB |
Output is correct |
27 |
Correct |
40 ms |
18396 KB |
Output is correct |
28 |
Correct |
44 ms |
18440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
16000 KB |
Output is correct |
2 |
Correct |
17 ms |
16000 KB |
Output is correct |
3 |
Correct |
19 ms |
16128 KB |
Output is correct |
4 |
Correct |
20 ms |
16256 KB |
Output is correct |
5 |
Correct |
19 ms |
16128 KB |
Output is correct |
6 |
Correct |
17 ms |
16128 KB |
Output is correct |
7 |
Correct |
17 ms |
16128 KB |
Output is correct |
8 |
Correct |
18 ms |
16128 KB |
Output is correct |
9 |
Correct |
27 ms |
16128 KB |
Output is correct |
10 |
Correct |
20 ms |
16128 KB |
Output is correct |
11 |
Correct |
18 ms |
16128 KB |
Output is correct |
12 |
Correct |
19 ms |
16128 KB |
Output is correct |
13 |
Correct |
19 ms |
16128 KB |
Output is correct |
14 |
Correct |
18 ms |
16128 KB |
Output is correct |
15 |
Correct |
22 ms |
16896 KB |
Output is correct |
16 |
Correct |
44 ms |
18376 KB |
Output is correct |
17 |
Correct |
40 ms |
18456 KB |
Output is correct |
18 |
Correct |
48 ms |
18528 KB |
Output is correct |
19 |
Correct |
39 ms |
18464 KB |
Output is correct |
20 |
Correct |
43 ms |
18356 KB |
Output is correct |
21 |
Correct |
44 ms |
18456 KB |
Output is correct |
22 |
Correct |
41 ms |
18420 KB |
Output is correct |
23 |
Correct |
46 ms |
18464 KB |
Output is correct |
24 |
Correct |
38 ms |
18348 KB |
Output is correct |
25 |
Correct |
43 ms |
18500 KB |
Output is correct |
26 |
Correct |
46 ms |
18416 KB |
Output is correct |
27 |
Correct |
40 ms |
18396 KB |
Output is correct |
28 |
Correct |
44 ms |
18440 KB |
Output is correct |
29 |
Correct |
3191 ms |
120620 KB |
Output is correct |
30 |
Correct |
3114 ms |
141824 KB |
Output is correct |
31 |
Correct |
3383 ms |
144424 KB |
Output is correct |
32 |
Correct |
3317 ms |
144492 KB |
Output is correct |
33 |
Correct |
2854 ms |
139428 KB |
Output is correct |
34 |
Correct |
3291 ms |
144748 KB |
Output is correct |
35 |
Correct |
3052 ms |
160292 KB |
Output is correct |
36 |
Correct |
2604 ms |
152816 KB |
Output is correct |
37 |
Correct |
3032 ms |
160124 KB |
Output is correct |