#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;
int n,m;
int mx=-INF,mn=INF;
bool solve(vector<vector<int>>&arr,int x,int y){
int Y=m;
vector<int> lim(n,0);
for(int i=0;i<n;i++){
for(int j=0;j<Y;j++){
if(arr[i][j]>x)Y=j;
}
lim[i]=Y;
}
for(int i=n-1;i>=0;i--){
for(int j=m-1;j>=lim[i];j--){
if(arr[i][j]<y)return false;
}
}
return true;
}
int binary_search(vector<vector<int>>& arr){
int l=0,r=mx-mn,ans=INF;
while(l<=r){
int mid=(l+r)/2;
if(solve(arr,mn+mid,mx-mid)){
ans=min(ans,mid);
r=mid-1;
}
else l=mid+1;
}
return ans;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("q.gir","r",stdin);
// freopen("q.cik","w",stdout);
cin>>n>>m;
vector<vector<int>>arr(n,vector<int>(m));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>arr[i][j];
int x=arr[i][j];
mn=min(mn,x);
mx=max(mx,x);
}
}
int ans=INF;
ans=min(ans,binary_search(arr));
reverse(all(arr));
ans=min(ans,binary_search(arr));
for(int i=0;i<n;i++)reverse(all(arr[i]));
ans=min(ans,binary_search(arr));
reverse(all(arr));
ans=min(ans,binary_search(arr));
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
252 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 |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
416 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
252 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 |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
416 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
760 KB |
Output is correct |
17 |
Correct |
7 ms |
888 KB |
Output is correct |
18 |
Correct |
8 ms |
888 KB |
Output is correct |
19 |
Correct |
8 ms |
916 KB |
Output is correct |
20 |
Correct |
7 ms |
892 KB |
Output is correct |
21 |
Correct |
10 ms |
1016 KB |
Output is correct |
22 |
Correct |
10 ms |
1016 KB |
Output is correct |
23 |
Correct |
10 ms |
1104 KB |
Output is correct |
24 |
Correct |
9 ms |
1016 KB |
Output is correct |
25 |
Correct |
10 ms |
1016 KB |
Output is correct |
26 |
Correct |
10 ms |
1016 KB |
Output is correct |
27 |
Correct |
10 ms |
988 KB |
Output is correct |
28 |
Correct |
10 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
252 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 |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
416 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
760 KB |
Output is correct |
17 |
Correct |
7 ms |
888 KB |
Output is correct |
18 |
Correct |
8 ms |
888 KB |
Output is correct |
19 |
Correct |
8 ms |
916 KB |
Output is correct |
20 |
Correct |
7 ms |
892 KB |
Output is correct |
21 |
Correct |
10 ms |
1016 KB |
Output is correct |
22 |
Correct |
10 ms |
1016 KB |
Output is correct |
23 |
Correct |
10 ms |
1104 KB |
Output is correct |
24 |
Correct |
9 ms |
1016 KB |
Output is correct |
25 |
Correct |
10 ms |
1016 KB |
Output is correct |
26 |
Correct |
10 ms |
1016 KB |
Output is correct |
27 |
Correct |
10 ms |
988 KB |
Output is correct |
28 |
Correct |
10 ms |
1016 KB |
Output is correct |
29 |
Correct |
458 ms |
52220 KB |
Output is correct |
30 |
Correct |
469 ms |
51100 KB |
Output is correct |
31 |
Correct |
493 ms |
55032 KB |
Output is correct |
32 |
Correct |
479 ms |
54904 KB |
Output is correct |
33 |
Correct |
424 ms |
47736 KB |
Output is correct |
34 |
Correct |
487 ms |
54904 KB |
Output is correct |
35 |
Correct |
785 ms |
70384 KB |
Output is correct |
36 |
Correct |
657 ms |
61532 KB |
Output is correct |
37 |
Correct |
792 ms |
70424 KB |
Output is correct |