#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;
if(!ok && !ok1)break;
// 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;
if(!ok && !ok1)break;
// 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 = Ma - Mi;
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:79:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + h >> 1;
~~^~~
joioi.cpp:68: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:69: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);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 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 |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
6 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 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 |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 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 |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
6 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 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 |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
8 ms |
1272 KB |
Output is correct |
17 |
Correct |
11 ms |
1276 KB |
Output is correct |
18 |
Correct |
11 ms |
1272 KB |
Output is correct |
19 |
Correct |
11 ms |
1272 KB |
Output is correct |
20 |
Correct |
11 ms |
1144 KB |
Output is correct |
21 |
Correct |
13 ms |
1272 KB |
Output is correct |
22 |
Correct |
12 ms |
1272 KB |
Output is correct |
23 |
Correct |
16 ms |
1272 KB |
Output is correct |
24 |
Correct |
12 ms |
1144 KB |
Output is correct |
25 |
Correct |
12 ms |
1268 KB |
Output is correct |
26 |
Correct |
13 ms |
1272 KB |
Output is correct |
27 |
Correct |
16 ms |
1272 KB |
Output is correct |
28 |
Correct |
15 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 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 |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
6 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 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 |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
8 ms |
1272 KB |
Output is correct |
17 |
Correct |
11 ms |
1276 KB |
Output is correct |
18 |
Correct |
11 ms |
1272 KB |
Output is correct |
19 |
Correct |
11 ms |
1272 KB |
Output is correct |
20 |
Correct |
11 ms |
1144 KB |
Output is correct |
21 |
Correct |
13 ms |
1272 KB |
Output is correct |
22 |
Correct |
12 ms |
1272 KB |
Output is correct |
23 |
Correct |
16 ms |
1272 KB |
Output is correct |
24 |
Correct |
12 ms |
1144 KB |
Output is correct |
25 |
Correct |
12 ms |
1268 KB |
Output is correct |
26 |
Correct |
13 ms |
1272 KB |
Output is correct |
27 |
Correct |
16 ms |
1272 KB |
Output is correct |
28 |
Correct |
15 ms |
1272 KB |
Output is correct |
29 |
Correct |
475 ms |
15184 KB |
Output is correct |
30 |
Correct |
472 ms |
16164 KB |
Output is correct |
31 |
Correct |
523 ms |
15992 KB |
Output is correct |
32 |
Correct |
473 ms |
16248 KB |
Output is correct |
33 |
Correct |
425 ms |
14164 KB |
Output is correct |
34 |
Correct |
496 ms |
16120 KB |
Output is correct |
35 |
Correct |
722 ms |
15992 KB |
Output is correct |
36 |
Correct |
611 ms |
15992 KB |
Output is correct |
37 |
Correct |
720 ms |
16280 KB |
Output is correct |