#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
const int N=2005;
int n, m, a[N][N], rens;
vector<int>v;
int num(bool x, bool y, int h){
int mx=1, mn=1e9;
if(x){
int r=(y ? m : -1);
for(int i=0;i<n;++i){
if(y){
int j=0;
for(;j<r;++j){
if(a[i][j]>h)
break;
}
r=j;
for(j=r;j<m;++j){
mn=min(mn, a[i][j]);
mx=max(mx, a[i][j]);
}
}
else{
int j=m-1;
for(;j>r;--j){
if(a[i][j]>h)
break;
}
r=j;
for(j=r;j>=0;--j){
mn=min(mn, a[i][j]);
mx=max(mx, a[i][j]);
}
}
}
}
else{
int r=(y ? m : -1);
for(int i=n-1;i>=0;--i){
if(y){
int j=0;
for(;j<r;++j){
if(a[i][j]>h)
break;
}
r=j;
for(j=r;j<m;++j){
mn=min(mn, a[i][j]);
mx=max(mx, a[i][j]);
}
}
else{
int j=m-1;
for(;j>r;--j){
if(a[i][j]>h)
break;
}
r=j;
for(j=r;j>=0;--j){
mn=min(mn, a[i][j]);
mx=max(mx, a[i][j]);
}
}
}
}
return mx-mn;
}
void ans(bool x, bool y){
int l=-1, r=v.size();
while(r>l+1){
int md=(l+r)/2;
int mm=num(x, y, v[md]);
if(mm<v[md]-v[0])
r=md;
else
l=md;
}
if(r<v.size())
rens=min(rens, v[r]-v[0]);
if(l>=0)
rens=min(rens, num(x, y, v[l]));
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
cin>>a[i][j];
v.push_back(a[i][j]);
}
}
sort(all(v));
v.resize(distance(v.begin(), unique(all(v))));
rens=2e9;
ans(0, 0);
ans(0, 1);
ans(1, 0);
ans(1, 1);
cout<<rens<<'\n';
}
Compilation message
joioi.cpp: In function 'void ans(bool, bool)':
joioi.cpp:85:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | if(r<v.size())
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
1624 KB |
Output is correct |
17 |
Correct |
10 ms |
1752 KB |
Output is correct |
18 |
Correct |
9 ms |
1756 KB |
Output is correct |
19 |
Correct |
9 ms |
1756 KB |
Output is correct |
20 |
Correct |
8 ms |
1748 KB |
Output is correct |
21 |
Correct |
10 ms |
1876 KB |
Output is correct |
22 |
Correct |
14 ms |
1880 KB |
Output is correct |
23 |
Correct |
11 ms |
1900 KB |
Output is correct |
24 |
Correct |
9 ms |
1752 KB |
Output is correct |
25 |
Correct |
10 ms |
1880 KB |
Output is correct |
26 |
Correct |
11 ms |
1880 KB |
Output is correct |
27 |
Correct |
11 ms |
1876 KB |
Output is correct |
28 |
Correct |
10 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
1624 KB |
Output is correct |
17 |
Correct |
10 ms |
1752 KB |
Output is correct |
18 |
Correct |
9 ms |
1756 KB |
Output is correct |
19 |
Correct |
9 ms |
1756 KB |
Output is correct |
20 |
Correct |
8 ms |
1748 KB |
Output is correct |
21 |
Correct |
10 ms |
1876 KB |
Output is correct |
22 |
Correct |
14 ms |
1880 KB |
Output is correct |
23 |
Correct |
11 ms |
1900 KB |
Output is correct |
24 |
Correct |
9 ms |
1752 KB |
Output is correct |
25 |
Correct |
10 ms |
1880 KB |
Output is correct |
26 |
Correct |
11 ms |
1880 KB |
Output is correct |
27 |
Correct |
11 ms |
1876 KB |
Output is correct |
28 |
Correct |
10 ms |
1880 KB |
Output is correct |
29 |
Correct |
819 ms |
32408 KB |
Output is correct |
30 |
Correct |
816 ms |
33364 KB |
Output is correct |
31 |
Correct |
906 ms |
34636 KB |
Output is correct |
32 |
Correct |
888 ms |
34628 KB |
Output is correct |
33 |
Correct |
757 ms |
29848 KB |
Output is correct |
34 |
Correct |
872 ms |
34784 KB |
Output is correct |
35 |
Correct |
1075 ms |
33304 KB |
Output is correct |
36 |
Correct |
968 ms |
43816 KB |
Output is correct |
37 |
Correct |
1070 ms |
32540 KB |
Output is correct |