// In The Name Of The Queen
#include<bits/stdc++.h>
using namespace std;
const int N = 2019, INF = 1e9 + 1;
int n, m, A[N][N], P[N], S[N], B[N];
pair < int , int > At[2];
int Val[2];
inline int Solve(int md)
{
for (int w = 0; w <= 1; w ++)
{
P[0] = 0; S[m + 1] = INF;
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= m; j ++)
P[j] = max(P[j - 1], A[i][j]);
for (int j = m; j; j --)
S[j] = min(S[j + 1], A[i][j]);
B[i] = m + 1;
int l = B[i - 1], r = m;
if (At[0].first == i)
l = max(l, At[0].second);
if (At[1].first == i)
r = min(r, At[1].second - 1);
for (int j = l; j <= r; j ++)
{
if (P[j] - Val[0] > md)
break;
if (Val[1] - S[j + 1] > md)
continue;
B[i] = j;
break;
}
}
if (B[n] <= m)
return 1;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++)
A[i][j] = INF - A[i][j];
Val[0] = INF - Val[0];
Val[1] = INF - Val[1];
swap(Val[0], Val[1]);
swap(At[0], At[1]);
}
return 0;
}
inline int Run()
{
Val[0] = INT_MAX;
Val[1] = INT_MIN;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++)
{
if (Val[0] > A[i][j])
Val[0] = A[i][j], At[0] = {i, j};
if (Val[1] < A[i][j])
Val[1] = A[i][j], At[1] = {i, j};
}
int le = -1, ri = (int)(1e9), md;
while (ri - le > 1)
{
md = (le + ri) >> 1;
if (Solve(md))
ri = md;
else
le = md;
}
return (ri);
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++)
scanf("%d", &A[i][j]);
int Mn = Run();
for (int i = 1; i < n - i + 1; i ++)
for (int j = 1; j <= m; j ++)
swap(A[i][j], A[n - i + 1][j]);
Mn = min(Mn, Run());
return !printf("%d\n", Mn);
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
joioi.cpp:75:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[i][j]);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
248 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
380 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
372 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
248 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
380 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
372 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
29 ms |
1404 KB |
Output is correct |
17 |
Correct |
38 ms |
1528 KB |
Output is correct |
18 |
Correct |
38 ms |
1632 KB |
Output is correct |
19 |
Correct |
36 ms |
1528 KB |
Output is correct |
20 |
Correct |
33 ms |
1400 KB |
Output is correct |
21 |
Correct |
46 ms |
1656 KB |
Output is correct |
22 |
Correct |
47 ms |
1740 KB |
Output is correct |
23 |
Correct |
43 ms |
1656 KB |
Output is correct |
24 |
Correct |
41 ms |
1528 KB |
Output is correct |
25 |
Correct |
44 ms |
1656 KB |
Output is correct |
26 |
Correct |
47 ms |
1656 KB |
Output is correct |
27 |
Correct |
46 ms |
1656 KB |
Output is correct |
28 |
Correct |
47 ms |
1656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
248 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
380 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
372 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
29 ms |
1404 KB |
Output is correct |
17 |
Correct |
38 ms |
1528 KB |
Output is correct |
18 |
Correct |
38 ms |
1632 KB |
Output is correct |
19 |
Correct |
36 ms |
1528 KB |
Output is correct |
20 |
Correct |
33 ms |
1400 KB |
Output is correct |
21 |
Correct |
46 ms |
1656 KB |
Output is correct |
22 |
Correct |
47 ms |
1740 KB |
Output is correct |
23 |
Correct |
43 ms |
1656 KB |
Output is correct |
24 |
Correct |
41 ms |
1528 KB |
Output is correct |
25 |
Correct |
44 ms |
1656 KB |
Output is correct |
26 |
Correct |
47 ms |
1656 KB |
Output is correct |
27 |
Correct |
46 ms |
1656 KB |
Output is correct |
28 |
Correct |
47 ms |
1656 KB |
Output is correct |
29 |
Correct |
3100 ms |
37488 KB |
Output is correct |
30 |
Correct |
2966 ms |
37760 KB |
Output is correct |
31 |
Correct |
3094 ms |
39400 KB |
Output is correct |
32 |
Correct |
3171 ms |
39160 KB |
Output is correct |
33 |
Correct |
2802 ms |
34412 KB |
Output is correct |
34 |
Correct |
3117 ms |
39480 KB |
Output is correct |
35 |
Execution timed out |
4006 ms |
33528 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |