#include<bits/stdc++.h>
using namespace std;
int mi, ma, N, M, a[2009][2009];
bool dp[2009][2009], ok[2009][2009];
pair < int, int > pf[2009][2009], sf[2009][2009];
pair < int, int > combine (pair < int, int > &a, int val)
{
return {min (a.first, val), max (a.second, val)};
}
bool check (int maxD)
{
int lft[2], rgt[2];
lft[0] = mi, rgt[0] = mi + maxD;
lft[1] = ma - maxD, rgt[1] = ma;
for (int up = 0; up < 2; up ++)
{
for (int j=1; j<=M; j++)
for (int i=0; i<=N; i++)
ok[j][i] = (pf[j][i].first >= lft[up] && pf[j][i].second <= rgt[up] &&
sf[j][i + 1].first >= lft[up ^ 1] && sf[j][i + 1].second <= rgt[up ^ 1]);
for (int direction = 0; direction < 2; direction ++)
{
for (int i=0; i<=N; i++)
dp[1][i] = ok[1][i] | (i > 0 ? dp[1][i - 1] : 0);
for (int i=2; i<=M; i++)
for (int j=0; j<=N; j++)
dp[i][j] = (ok[i][j] & dp[i - 1][j]) | (j > 0 ? dp[i][j - 1] : 0);
if (dp[M][N]) return 1;
for (int i=1; i<=M; i++)
reverse (ok[i], ok[i] + N + 1);
}
}
return 0;
}
int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);
scanf ("%d %d", &N, &M);
for (int i=1; i<=N; i++)
for (int j=1; j<=M; j++)
{
scanf ("%d", &a[i][j]);
if (i + j == 2) mi = ma = a[i][j];
else mi = min (mi, a[i][j]), ma = max (ma, a[i][j]);
}
for (int j=1; j<=M; j++)
{
pf[j][0] = {ma, mi}, sf[j][N + 1] = {ma, mi};
for (int i=1; i<=N; i++)
pf[j][i] = combine (pf[j][i - 1], a[i][j]);
for (int i=N; i>=1; i--)
sf[j][i] = combine (sf[j][i + 1], a[i][j]);
}
int p = 0, u = ma - mi - 1, mij, ras = ma - mi;
while (p <= u)
{
mij = (p + u) >> 1;
if (check (mij)) ras = mij, u = mij - 1;
else p = mij + 1;
}
printf ("%d\n", ras);
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:45:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d %d", &N, &M);
~~~~~~^~~~~~~~~~~~~~~~~
joioi.cpp:49:15: 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 |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
560 KB |
Output is correct |
4 |
Correct |
3 ms |
764 KB |
Output is correct |
5 |
Correct |
3 ms |
828 KB |
Output is correct |
6 |
Correct |
3 ms |
828 KB |
Output is correct |
7 |
Correct |
3 ms |
832 KB |
Output is correct |
8 |
Correct |
3 ms |
832 KB |
Output is correct |
9 |
Correct |
2 ms |
832 KB |
Output is correct |
10 |
Correct |
2 ms |
832 KB |
Output is correct |
11 |
Correct |
2 ms |
832 KB |
Output is correct |
12 |
Correct |
2 ms |
832 KB |
Output is correct |
13 |
Correct |
2 ms |
832 KB |
Output is correct |
14 |
Correct |
2 ms |
832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
560 KB |
Output is correct |
4 |
Correct |
3 ms |
764 KB |
Output is correct |
5 |
Correct |
3 ms |
828 KB |
Output is correct |
6 |
Correct |
3 ms |
828 KB |
Output is correct |
7 |
Correct |
3 ms |
832 KB |
Output is correct |
8 |
Correct |
3 ms |
832 KB |
Output is correct |
9 |
Correct |
2 ms |
832 KB |
Output is correct |
10 |
Correct |
2 ms |
832 KB |
Output is correct |
11 |
Correct |
2 ms |
832 KB |
Output is correct |
12 |
Correct |
2 ms |
832 KB |
Output is correct |
13 |
Correct |
2 ms |
832 KB |
Output is correct |
14 |
Correct |
2 ms |
832 KB |
Output is correct |
15 |
Correct |
5 ms |
3024 KB |
Output is correct |
16 |
Correct |
10 ms |
4304 KB |
Output is correct |
17 |
Correct |
22 ms |
4604 KB |
Output is correct |
18 |
Correct |
29 ms |
4604 KB |
Output is correct |
19 |
Correct |
20 ms |
4604 KB |
Output is correct |
20 |
Correct |
20 ms |
4604 KB |
Output is correct |
21 |
Correct |
26 ms |
4604 KB |
Output is correct |
22 |
Correct |
27 ms |
4604 KB |
Output is correct |
23 |
Correct |
24 ms |
4604 KB |
Output is correct |
24 |
Correct |
22 ms |
4604 KB |
Output is correct |
25 |
Correct |
21 ms |
4604 KB |
Output is correct |
26 |
Correct |
36 ms |
4604 KB |
Output is correct |
27 |
Correct |
30 ms |
4628 KB |
Output is correct |
28 |
Correct |
30 ms |
4732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
560 KB |
Output is correct |
4 |
Correct |
3 ms |
764 KB |
Output is correct |
5 |
Correct |
3 ms |
828 KB |
Output is correct |
6 |
Correct |
3 ms |
828 KB |
Output is correct |
7 |
Correct |
3 ms |
832 KB |
Output is correct |
8 |
Correct |
3 ms |
832 KB |
Output is correct |
9 |
Correct |
2 ms |
832 KB |
Output is correct |
10 |
Correct |
2 ms |
832 KB |
Output is correct |
11 |
Correct |
2 ms |
832 KB |
Output is correct |
12 |
Correct |
2 ms |
832 KB |
Output is correct |
13 |
Correct |
2 ms |
832 KB |
Output is correct |
14 |
Correct |
2 ms |
832 KB |
Output is correct |
15 |
Correct |
5 ms |
3024 KB |
Output is correct |
16 |
Correct |
10 ms |
4304 KB |
Output is correct |
17 |
Correct |
22 ms |
4604 KB |
Output is correct |
18 |
Correct |
29 ms |
4604 KB |
Output is correct |
19 |
Correct |
20 ms |
4604 KB |
Output is correct |
20 |
Correct |
20 ms |
4604 KB |
Output is correct |
21 |
Correct |
26 ms |
4604 KB |
Output is correct |
22 |
Correct |
27 ms |
4604 KB |
Output is correct |
23 |
Correct |
24 ms |
4604 KB |
Output is correct |
24 |
Correct |
22 ms |
4604 KB |
Output is correct |
25 |
Correct |
21 ms |
4604 KB |
Output is correct |
26 |
Correct |
36 ms |
4604 KB |
Output is correct |
27 |
Correct |
30 ms |
4628 KB |
Output is correct |
28 |
Correct |
30 ms |
4732 KB |
Output is correct |
29 |
Correct |
1445 ms |
86396 KB |
Output is correct |
30 |
Correct |
1662 ms |
86396 KB |
Output is correct |
31 |
Correct |
1584 ms |
87164 KB |
Output is correct |
32 |
Correct |
1648 ms |
87164 KB |
Output is correct |
33 |
Correct |
1187 ms |
87164 KB |
Output is correct |
34 |
Correct |
1552 ms |
87164 KB |
Output is correct |
35 |
Correct |
2524 ms |
87292 KB |
Output is correct |
36 |
Correct |
1757 ms |
112124 KB |
Output is correct |
37 |
Correct |
2688 ms |
159856 KB |
Output is correct |