#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define taskname "A"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
const int maxn = 2e3 + 5;
int n , m , a[maxn][maxn] , Mi = 1e9 , Ma = 0;
int res = 0;
bool check(int val){
int L = 1 , R = m;
bool ok = 1 , ok1 = 1;
for(int i = 1 ; i <= n ; ++i){
int l = m , r = 1;
int Mi = 1e9 , Ma = 0;
for( ; l >= 1 ; --l){
Ma = max(Ma , a[i][l]);
if(Ma - ::Mi > val)break;
}
for( ; r <= m ; ++r){
Mi = min(Mi , a[i][r]);
if(::Ma - Mi > val)break;
}
--r;++l;
R = min(R , r);
if(R < l - 1)ok = 0;
L = max(L , l);
if(r + 1 < L)ok1 = 0;
// cout << l << " " << r << endl;
}
// cout << endl;
if(ok || ok1)return 1;
ok = 1 , ok1 = 1;
L = 1;R = m;
for(int i = 1 ; i <= n ; ++i){
int l = m , r = 1;
int Mi = 1e9 , Ma = 0;
for( ; l >= 1 ; --l){
Mi = min(Mi , a[i][l]);
if(::Ma - Mi > val)break;
}
for( ; r <= m ; ++r){
Ma = max(Ma , a[i][r]);
if(Ma - ::Mi > val)break;
}
--r;++l;
R = min(R , r);
if(R < l - 1)ok = 0;
L = max(L , l);
if(r + 1 < L)ok1 = 0;
// cout << l << " " << r << endl;
}
if(ok || ok1)return 1;
return 0;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP", "r",stdin);
freopen(taskname".OUT", "w",stdout);
}
cin >> n >> m;
for(int i = 1 ; i <= n ; ++i)for(int j = 1 ; j <= m ; ++j){
cin >> a[i][j];
Mi = min(Mi,a[i][j]);Ma = max(Ma,a[i][j]);
}
// return cout << check(1) , 0;
int l = 0, h = 1e9;
while(l <= h){
int mid = l + h >> 1;
if(check(mid))h = mid - 1;
else l = mid + 1;
}
cout << min(l , Ma - Mi);
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:77:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + h >> 1;
~~^~~
joioi.cpp:66:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".INP", "r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:67:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".OUT", "w",stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
428 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
380 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
428 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
380 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
380 KB |
Output is correct |
16 |
Correct |
11 ms |
1400 KB |
Output is correct |
17 |
Correct |
20 ms |
1528 KB |
Output is correct |
18 |
Correct |
13 ms |
1528 KB |
Output is correct |
19 |
Correct |
14 ms |
1528 KB |
Output is correct |
20 |
Correct |
13 ms |
1400 KB |
Output is correct |
21 |
Correct |
17 ms |
1784 KB |
Output is correct |
22 |
Correct |
14 ms |
1656 KB |
Output is correct |
23 |
Correct |
15 ms |
1656 KB |
Output is correct |
24 |
Correct |
13 ms |
1528 KB |
Output is correct |
25 |
Correct |
14 ms |
1660 KB |
Output is correct |
26 |
Correct |
17 ms |
1656 KB |
Output is correct |
27 |
Correct |
18 ms |
1656 KB |
Output is correct |
28 |
Correct |
16 ms |
1656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
428 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
380 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
380 KB |
Output is correct |
16 |
Correct |
11 ms |
1400 KB |
Output is correct |
17 |
Correct |
20 ms |
1528 KB |
Output is correct |
18 |
Correct |
13 ms |
1528 KB |
Output is correct |
19 |
Correct |
14 ms |
1528 KB |
Output is correct |
20 |
Correct |
13 ms |
1400 KB |
Output is correct |
21 |
Correct |
17 ms |
1784 KB |
Output is correct |
22 |
Correct |
14 ms |
1656 KB |
Output is correct |
23 |
Correct |
15 ms |
1656 KB |
Output is correct |
24 |
Correct |
13 ms |
1528 KB |
Output is correct |
25 |
Correct |
14 ms |
1660 KB |
Output is correct |
26 |
Correct |
17 ms |
1656 KB |
Output is correct |
27 |
Correct |
18 ms |
1656 KB |
Output is correct |
28 |
Correct |
16 ms |
1656 KB |
Output is correct |
29 |
Correct |
720 ms |
37568 KB |
Output is correct |
30 |
Correct |
680 ms |
37752 KB |
Output is correct |
31 |
Correct |
758 ms |
39420 KB |
Output is correct |
32 |
Correct |
756 ms |
39288 KB |
Output is correct |
33 |
Correct |
627 ms |
34332 KB |
Output is correct |
34 |
Correct |
785 ms |
39416 KB |
Output is correct |
35 |
Correct |
818 ms |
54904 KB |
Output is correct |
36 |
Correct |
770 ms |
49732 KB |
Output is correct |
37 |
Correct |
831 ms |
55032 KB |
Output is correct |