#include <bits/stdc++.h>
using namespace std;
//#define FILE_IO
int N, M, ans, cost;
int mn, mx;
int pfxmn[2005], sfxmx[2005];
int aux[2005][2005];
int a[2005][2005];
int dp[2005][2005];
void solve()
{
for(int i = 1; i <= N; i++)
{
pfxmn[0] = 1 << 30;
for(int j = 1; j <= M; j++) pfxmn[j] = min(pfxmn[j - 1], a[i][j]);
sfxmx[M + 1] = -1;
for(int j = M; j >= 1; j--) sfxmx[j] = max(sfxmx[j + 1], a[i][j]);
for(int j = M; j >= 0; j--)
{
cost = max(mx - pfxmn[j], sfxmx[j + 1] - mn);
dp[i][j] = max(dp[i - 1][j], cost);
if(j < M) dp[i][j] = min(dp[i][j], dp[i][j + 1]);
}
}
ans = min(ans, dp[N][0]);
}
void rotate90()
{
for(int i = 1; i <= N; i++)
for(int j = 1; j <= M; j++)
{
aux[i][j] = a[i][j];
a[i][j] = 0;
}
for(int i = 1; i <= N; i++)
for(int j = 1; j <= M; j++)
a[j][N - i + 1] = aux[i][j];
swap(N, M);
}
int main()
{
#ifdef FILE_IO
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
scanf("%d%d", &N, &M);
mn = 1 << 30, mx = -1;
for(int i = 1; i <= N; i++)
for(int j = 1; j <= M; j++)
{
scanf("%d", &a[i][j]);
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
ans = mx - mn;
solve();
rotate90();
solve();
rotate90();
solve();
rotate90();
solve();
printf("%d\n", ans);
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:54: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:59:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i][j]);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
656 KB |
Output is correct |
3 |
Correct |
2 ms |
656 KB |
Output is correct |
4 |
Correct |
2 ms |
656 KB |
Output is correct |
5 |
Correct |
2 ms |
656 KB |
Output is correct |
6 |
Correct |
2 ms |
656 KB |
Output is correct |
7 |
Correct |
2 ms |
656 KB |
Output is correct |
8 |
Correct |
2 ms |
700 KB |
Output is correct |
9 |
Correct |
2 ms |
720 KB |
Output is correct |
10 |
Correct |
2 ms |
848 KB |
Output is correct |
11 |
Correct |
2 ms |
848 KB |
Output is correct |
12 |
Correct |
2 ms |
848 KB |
Output is correct |
13 |
Correct |
2 ms |
848 KB |
Output is correct |
14 |
Correct |
2 ms |
848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
656 KB |
Output is correct |
3 |
Correct |
2 ms |
656 KB |
Output is correct |
4 |
Correct |
2 ms |
656 KB |
Output is correct |
5 |
Correct |
2 ms |
656 KB |
Output is correct |
6 |
Correct |
2 ms |
656 KB |
Output is correct |
7 |
Correct |
2 ms |
656 KB |
Output is correct |
8 |
Correct |
2 ms |
700 KB |
Output is correct |
9 |
Correct |
2 ms |
720 KB |
Output is correct |
10 |
Correct |
2 ms |
848 KB |
Output is correct |
11 |
Correct |
2 ms |
848 KB |
Output is correct |
12 |
Correct |
2 ms |
848 KB |
Output is correct |
13 |
Correct |
2 ms |
848 KB |
Output is correct |
14 |
Correct |
2 ms |
848 KB |
Output is correct |
15 |
Correct |
5 ms |
3100 KB |
Output is correct |
16 |
Correct |
9 ms |
3500 KB |
Output is correct |
17 |
Correct |
11 ms |
3832 KB |
Output is correct |
18 |
Correct |
12 ms |
4232 KB |
Output is correct |
19 |
Correct |
12 ms |
4388 KB |
Output is correct |
20 |
Correct |
11 ms |
4660 KB |
Output is correct |
21 |
Correct |
13 ms |
5028 KB |
Output is correct |
22 |
Correct |
12 ms |
5416 KB |
Output is correct |
23 |
Correct |
13 ms |
5800 KB |
Output is correct |
24 |
Correct |
12 ms |
6136 KB |
Output is correct |
25 |
Correct |
14 ms |
6532 KB |
Output is correct |
26 |
Correct |
14 ms |
6920 KB |
Output is correct |
27 |
Correct |
13 ms |
7248 KB |
Output is correct |
28 |
Correct |
13 ms |
7712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
656 KB |
Output is correct |
3 |
Correct |
2 ms |
656 KB |
Output is correct |
4 |
Correct |
2 ms |
656 KB |
Output is correct |
5 |
Correct |
2 ms |
656 KB |
Output is correct |
6 |
Correct |
2 ms |
656 KB |
Output is correct |
7 |
Correct |
2 ms |
656 KB |
Output is correct |
8 |
Correct |
2 ms |
700 KB |
Output is correct |
9 |
Correct |
2 ms |
720 KB |
Output is correct |
10 |
Correct |
2 ms |
848 KB |
Output is correct |
11 |
Correct |
2 ms |
848 KB |
Output is correct |
12 |
Correct |
2 ms |
848 KB |
Output is correct |
13 |
Correct |
2 ms |
848 KB |
Output is correct |
14 |
Correct |
2 ms |
848 KB |
Output is correct |
15 |
Correct |
5 ms |
3100 KB |
Output is correct |
16 |
Correct |
9 ms |
3500 KB |
Output is correct |
17 |
Correct |
11 ms |
3832 KB |
Output is correct |
18 |
Correct |
12 ms |
4232 KB |
Output is correct |
19 |
Correct |
12 ms |
4388 KB |
Output is correct |
20 |
Correct |
11 ms |
4660 KB |
Output is correct |
21 |
Correct |
13 ms |
5028 KB |
Output is correct |
22 |
Correct |
12 ms |
5416 KB |
Output is correct |
23 |
Correct |
13 ms |
5800 KB |
Output is correct |
24 |
Correct |
12 ms |
6136 KB |
Output is correct |
25 |
Correct |
14 ms |
6532 KB |
Output is correct |
26 |
Correct |
14 ms |
6920 KB |
Output is correct |
27 |
Correct |
13 ms |
7248 KB |
Output is correct |
28 |
Correct |
13 ms |
7712 KB |
Output is correct |
29 |
Correct |
877 ms |
73964 KB |
Output is correct |
30 |
Correct |
813 ms |
81308 KB |
Output is correct |
31 |
Correct |
864 ms |
81468 KB |
Output is correct |
32 |
Correct |
968 ms |
81468 KB |
Output is correct |
33 |
Correct |
796 ms |
81468 KB |
Output is correct |
34 |
Correct |
920 ms |
81468 KB |
Output is correct |
35 |
Correct |
1166 ms |
81468 KB |
Output is correct |
36 |
Correct |
922 ms |
81468 KB |
Output is correct |
37 |
Correct |
1041 ms |
81468 KB |
Output is correct |