#include <bits/stdc++.h>
using namespace std;
int n, m;
int Min, Max;
int a[2005][2005];
inline bool check(int dif, bool dir1, bool dir2){
int i, j = 1;
int adc = 1, adl = 1, Last = n;
if(dir1 == 1) adc = -1, j = m;
if(dir2 == 1) adl = -1, Last = 1;
int mx = 0, mn = 1e9;
while(j <= m && j >= 1){
i = 1;
if(dir2) i = n;
while(Min + dif >= a[i][j] && (Last - i) * adl >= 0) i += adl;
Last = i - adl;
while(i <= n && i >= 1){
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
i += adl;
}
j += adc;
}
if(mx - mn <= dif) return 1;
return 0;
}
inline int solve(){
int st = 0, dr = Max - Min;
while(st <= dr){
int mij = (st + dr) / 2;
bool ok = false;
for(int dir1 = 0; dir1 < 2 ; ++dir1)
for(int dir2 = 0; dir2 < 2 ; ++dir2)
ok |= check(mij, dir1, dir2);
if(ok) dr = mij - 1;
else st = mij + 1;
}
return st;
}
int main()
{
scanf("%d%d", &n, &m);
Min = 1e9; Max = 0;
for(int i = 1; i <= n ; ++i){
for(int j = 1; j <= m ; ++j){
scanf("%d", &a[i][j]);
Min = min(Min, a[i][j]);
Max = max(Max, a[i][j]);
}
}
int Sol = solve();
printf("%d", Sol);
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:55: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:60: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 |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
420 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
404 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
400 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
420 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
404 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
400 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
7 ms |
1400 KB |
Output is correct |
17 |
Correct |
14 ms |
1528 KB |
Output is correct |
18 |
Correct |
15 ms |
1528 KB |
Output is correct |
19 |
Correct |
14 ms |
1528 KB |
Output is correct |
20 |
Correct |
13 ms |
1404 KB |
Output is correct |
21 |
Correct |
18 ms |
1656 KB |
Output is correct |
22 |
Correct |
18 ms |
1656 KB |
Output is correct |
23 |
Correct |
17 ms |
1784 KB |
Output is correct |
24 |
Correct |
14 ms |
1528 KB |
Output is correct |
25 |
Correct |
17 ms |
1656 KB |
Output is correct |
26 |
Correct |
17 ms |
1656 KB |
Output is correct |
27 |
Correct |
17 ms |
1656 KB |
Output is correct |
28 |
Correct |
17 ms |
1656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
420 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
404 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
400 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
7 ms |
1400 KB |
Output is correct |
17 |
Correct |
14 ms |
1528 KB |
Output is correct |
18 |
Correct |
15 ms |
1528 KB |
Output is correct |
19 |
Correct |
14 ms |
1528 KB |
Output is correct |
20 |
Correct |
13 ms |
1404 KB |
Output is correct |
21 |
Correct |
18 ms |
1656 KB |
Output is correct |
22 |
Correct |
18 ms |
1656 KB |
Output is correct |
23 |
Correct |
17 ms |
1784 KB |
Output is correct |
24 |
Correct |
14 ms |
1528 KB |
Output is correct |
25 |
Correct |
17 ms |
1656 KB |
Output is correct |
26 |
Correct |
17 ms |
1656 KB |
Output is correct |
27 |
Correct |
17 ms |
1656 KB |
Output is correct |
28 |
Correct |
17 ms |
1656 KB |
Output is correct |
29 |
Correct |
2509 ms |
37484 KB |
Output is correct |
30 |
Correct |
2349 ms |
37696 KB |
Output is correct |
31 |
Correct |
2805 ms |
39164 KB |
Output is correct |
32 |
Correct |
2717 ms |
39184 KB |
Output is correct |
33 |
Correct |
2403 ms |
34228 KB |
Output is correct |
34 |
Correct |
2670 ms |
39244 KB |
Output is correct |
35 |
Execution timed out |
4021 ms |
54988 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |