#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long lld;
const int MX=2010, inf=2e9;
int B[MX][MX];
int w, h;
vector<int> X;
int solve(){
multiset<int> J;
for(int i=1; i<=h; i++)
for(int j=1; j<=w; j++)
J.insert(B[i][j]);
int pos[MX]={};
int mn=inf, mx=0, ans=inf;
for(int x:X){
for(int i=1; i<=h; i++){
while(pos[i]<w && B[i][pos[i]+1]<=x){
int now=B[i][pos[i]+1];
pos[i]++;
mn=min(mn, now); mx=max(mx, now);
J.erase(J.find(now));
}
}
if((int)J.size()==w*h || J.empty()) continue;
ans=min(ans, max(mx-mn, *J.rbegin()-*J.begin()));
}
return ans;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>h>>w;
for(int i=1; i<=h; i++)
for(int j=1; j<=w; j++)
cin>>B[i][j], X.push_back(B[i][j]);
sort(X.begin(), X.end());
X.resize(unique(X.begin(), X.end())-X.begin());
int ans=solve();
for(int i=1; i<=h; i++)
reverse(B[i]+1, B[i]+w+1);
ans=min(ans, solve());
for(int j=1; j<=w; j++)
for(int i=1; i<=h/2; i++)
swap(B[i][j], B[h-i][j]);
ans=min(ans, solve());
for(int i=1; i<=h; i++)
reverse(B[i]+1, B[i]+w+1);
ans=min(ans, solve());
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |