#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], sf[2009];
pair < int, int > combine (pair < int, int > a, pair < int, int > b)
{
return {min (a.first, b.first), max (a.second, b.second)};
}
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++)
{
pf[0] = {ma, mi}, sf[N + 1] = {ma, mi};
for (int i=1; i<=N; i++)
pf[i] = combine (pf[i - 1], {a[i][j], a[i][j]});
for (int i=N; i>=1; i--)
sf[i] = combine (sf[i + 1], {a[i][j], a[i][j]});
for (int i=0; i<=N; i++)
ok[j][i] = (pf[i].first >= lft[up] && pf[i].second <= rgt[up] &&
sf[i + 1].first >= lft[up ^ 1] && sf[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]);
}
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:52: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:56:15: 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 |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
644 KB |
Output is correct |
6 |
Correct |
3 ms |
644 KB |
Output is correct |
7 |
Correct |
2 ms |
760 KB |
Output is correct |
8 |
Correct |
3 ms |
760 KB |
Output is correct |
9 |
Correct |
3 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
796 KB |
Output is correct |
11 |
Correct |
3 ms |
796 KB |
Output is correct |
12 |
Correct |
3 ms |
796 KB |
Output is correct |
13 |
Correct |
2 ms |
796 KB |
Output is correct |
14 |
Correct |
2 ms |
796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
644 KB |
Output is correct |
6 |
Correct |
3 ms |
644 KB |
Output is correct |
7 |
Correct |
2 ms |
760 KB |
Output is correct |
8 |
Correct |
3 ms |
760 KB |
Output is correct |
9 |
Correct |
3 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
796 KB |
Output is correct |
11 |
Correct |
3 ms |
796 KB |
Output is correct |
12 |
Correct |
3 ms |
796 KB |
Output is correct |
13 |
Correct |
2 ms |
796 KB |
Output is correct |
14 |
Correct |
2 ms |
796 KB |
Output is correct |
15 |
Correct |
4 ms |
1524 KB |
Output is correct |
16 |
Correct |
13 ms |
2556 KB |
Output is correct |
17 |
Correct |
27 ms |
2904 KB |
Output is correct |
18 |
Correct |
25 ms |
3064 KB |
Output is correct |
19 |
Correct |
44 ms |
3460 KB |
Output is correct |
20 |
Correct |
32 ms |
3492 KB |
Output is correct |
21 |
Correct |
36 ms |
4004 KB |
Output is correct |
22 |
Correct |
42 ms |
4476 KB |
Output is correct |
23 |
Correct |
31 ms |
4836 KB |
Output is correct |
24 |
Correct |
36 ms |
5036 KB |
Output is correct |
25 |
Correct |
31 ms |
5508 KB |
Output is correct |
26 |
Correct |
39 ms |
5896 KB |
Output is correct |
27 |
Correct |
39 ms |
6300 KB |
Output is correct |
28 |
Correct |
41 ms |
6688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
644 KB |
Output is correct |
6 |
Correct |
3 ms |
644 KB |
Output is correct |
7 |
Correct |
2 ms |
760 KB |
Output is correct |
8 |
Correct |
3 ms |
760 KB |
Output is correct |
9 |
Correct |
3 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
796 KB |
Output is correct |
11 |
Correct |
3 ms |
796 KB |
Output is correct |
12 |
Correct |
3 ms |
796 KB |
Output is correct |
13 |
Correct |
2 ms |
796 KB |
Output is correct |
14 |
Correct |
2 ms |
796 KB |
Output is correct |
15 |
Correct |
4 ms |
1524 KB |
Output is correct |
16 |
Correct |
13 ms |
2556 KB |
Output is correct |
17 |
Correct |
27 ms |
2904 KB |
Output is correct |
18 |
Correct |
25 ms |
3064 KB |
Output is correct |
19 |
Correct |
44 ms |
3460 KB |
Output is correct |
20 |
Correct |
32 ms |
3492 KB |
Output is correct |
21 |
Correct |
36 ms |
4004 KB |
Output is correct |
22 |
Correct |
42 ms |
4476 KB |
Output is correct |
23 |
Correct |
31 ms |
4836 KB |
Output is correct |
24 |
Correct |
36 ms |
5036 KB |
Output is correct |
25 |
Correct |
31 ms |
5508 KB |
Output is correct |
26 |
Correct |
39 ms |
5896 KB |
Output is correct |
27 |
Correct |
39 ms |
6300 KB |
Output is correct |
28 |
Correct |
41 ms |
6688 KB |
Output is correct |
29 |
Correct |
3070 ms |
50112 KB |
Output is correct |
30 |
Correct |
3444 ms |
71912 KB |
Output is correct |
31 |
Correct |
3726 ms |
95652 KB |
Output is correct |
32 |
Correct |
3796 ms |
118888 KB |
Output is correct |
33 |
Correct |
2804 ms |
136956 KB |
Output is correct |
34 |
Correct |
3758 ms |
162164 KB |
Output is correct |
35 |
Execution timed out |
4042 ms |
200672 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |