#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()
{
priority_queue <pair<int,pair<int,int>>, vector<pair<int,pair<int,int>>>, greater<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({A[i][y],{i,y}});
G[i][y] = 1;
Max = max(A[i][y], Max);
Min = min(A[i][y], Min);
}
}
Re = Max - Min;
Mi2 = Min;
while (Q.size() > 0)
{
I = Q.top().second.first;
Y = Q.top().second.second;
a = Q.top().first;
Q.pop();
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 |
16128 KB |
Output is correct |
2 |
Correct |
17 ms |
16128 KB |
Output is correct |
3 |
Correct |
18 ms |
16128 KB |
Output is correct |
4 |
Correct |
19 ms |
16128 KB |
Output is correct |
5 |
Correct |
18 ms |
16128 KB |
Output is correct |
6 |
Correct |
18 ms |
16128 KB |
Output is correct |
7 |
Correct |
20 ms |
16128 KB |
Output is correct |
8 |
Correct |
19 ms |
16128 KB |
Output is correct |
9 |
Correct |
18 ms |
16128 KB |
Output is correct |
10 |
Correct |
19 ms |
16128 KB |
Output is correct |
11 |
Correct |
19 ms |
16128 KB |
Output is correct |
12 |
Correct |
18 ms |
16128 KB |
Output is correct |
13 |
Correct |
18 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 |
16128 KB |
Output is correct |
2 |
Correct |
17 ms |
16128 KB |
Output is correct |
3 |
Correct |
18 ms |
16128 KB |
Output is correct |
4 |
Correct |
19 ms |
16128 KB |
Output is correct |
5 |
Correct |
18 ms |
16128 KB |
Output is correct |
6 |
Correct |
18 ms |
16128 KB |
Output is correct |
7 |
Correct |
20 ms |
16128 KB |
Output is correct |
8 |
Correct |
19 ms |
16128 KB |
Output is correct |
9 |
Correct |
18 ms |
16128 KB |
Output is correct |
10 |
Correct |
19 ms |
16128 KB |
Output is correct |
11 |
Correct |
19 ms |
16128 KB |
Output is correct |
12 |
Correct |
18 ms |
16128 KB |
Output is correct |
13 |
Correct |
18 ms |
16128 KB |
Output is correct |
14 |
Correct |
18 ms |
16128 KB |
Output is correct |
15 |
Correct |
21 ms |
16896 KB |
Output is correct |
16 |
Correct |
50 ms |
18440 KB |
Output is correct |
17 |
Correct |
53 ms |
18712 KB |
Output is correct |
18 |
Correct |
51 ms |
18792 KB |
Output is correct |
19 |
Correct |
54 ms |
18712 KB |
Output is correct |
20 |
Correct |
48 ms |
18612 KB |
Output is correct |
21 |
Correct |
54 ms |
18848 KB |
Output is correct |
22 |
Correct |
52 ms |
18792 KB |
Output is correct |
23 |
Correct |
52 ms |
18848 KB |
Output is correct |
24 |
Correct |
49 ms |
18732 KB |
Output is correct |
25 |
Correct |
52 ms |
18848 KB |
Output is correct |
26 |
Correct |
55 ms |
18840 KB |
Output is correct |
27 |
Correct |
54 ms |
18840 KB |
Output is correct |
28 |
Correct |
52 ms |
18848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
16128 KB |
Output is correct |
2 |
Correct |
17 ms |
16128 KB |
Output is correct |
3 |
Correct |
18 ms |
16128 KB |
Output is correct |
4 |
Correct |
19 ms |
16128 KB |
Output is correct |
5 |
Correct |
18 ms |
16128 KB |
Output is correct |
6 |
Correct |
18 ms |
16128 KB |
Output is correct |
7 |
Correct |
20 ms |
16128 KB |
Output is correct |
8 |
Correct |
19 ms |
16128 KB |
Output is correct |
9 |
Correct |
18 ms |
16128 KB |
Output is correct |
10 |
Correct |
19 ms |
16128 KB |
Output is correct |
11 |
Correct |
19 ms |
16128 KB |
Output is correct |
12 |
Correct |
18 ms |
16128 KB |
Output is correct |
13 |
Correct |
18 ms |
16128 KB |
Output is correct |
14 |
Correct |
18 ms |
16128 KB |
Output is correct |
15 |
Correct |
21 ms |
16896 KB |
Output is correct |
16 |
Correct |
50 ms |
18440 KB |
Output is correct |
17 |
Correct |
53 ms |
18712 KB |
Output is correct |
18 |
Correct |
51 ms |
18792 KB |
Output is correct |
19 |
Correct |
54 ms |
18712 KB |
Output is correct |
20 |
Correct |
48 ms |
18612 KB |
Output is correct |
21 |
Correct |
54 ms |
18848 KB |
Output is correct |
22 |
Correct |
52 ms |
18792 KB |
Output is correct |
23 |
Correct |
52 ms |
18848 KB |
Output is correct |
24 |
Correct |
49 ms |
18732 KB |
Output is correct |
25 |
Correct |
52 ms |
18848 KB |
Output is correct |
26 |
Correct |
55 ms |
18840 KB |
Output is correct |
27 |
Correct |
54 ms |
18840 KB |
Output is correct |
28 |
Correct |
52 ms |
18848 KB |
Output is correct |
29 |
Execution timed out |
4077 ms |
142860 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |