#pragma gcc optimize ("Ofast")
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
#define pii pair<int,int>
#define F first
#define S second
using namespace std;
const int maxN = 2e3 + 326, INF = 2e9 + 326;
int N, M, arr[maxN][maxN], sz, ii, jj;
vector<int> lisan, cnt;
vector<vector<pii>> pos;
bool vis[maxN][maxN];
inline void take(int &y, int &x, int &mx, int &mn, int &d){
if(d == 0){
for(ii = y; ii >= 0 && !vis[ii][x]; ii--){
for(jj= x; jj >= 0 && !vis[ii][jj]; jj--){
vis[ii][jj] = true;
cnt[arr[ii][jj]]--;
mx = max(mx, arr[ii][jj]);
mn = min(mn, arr[ii][jj]);
}
}
} else if(d == 1){
for(ii = y; ii < N && !vis[ii][x]; ii++){
for(jj= x; jj >= 0 && !vis[ii][jj]; jj--){
vis[ii][jj] = true;
cnt[arr[ii][jj]]--;
mx = max(mx, arr[ii][jj]);
mn = min(mn, arr[ii][jj]);
}
}
} else if(d == 2){
for(ii = y; ii < N && !vis[ii][x]; ii++){
for(jj= x; jj < M && !vis[ii][jj]; jj++){
vis[ii][jj] = true;
cnt[arr[ii][jj]]--;
mx = max(mx, arr[ii][jj]);
mn = min(mn, arr[ii][jj]);
}
}
} else if(d == 3){
for(ii = y; ii >= 0 && !vis[ii][x]; ii--){
for(jj= x; jj < M && !vis[ii][jj]; jj++){
vis[ii][jj] = true;
cnt[arr[ii][jj]]--;
mx = max(mx, arr[ii][jj]);
mn = min(mn, arr[ii][jj]);
}
}
}
}
inline void rot(){
for(ii = 0; i < sz; i++){
cnt[ii] = 0;
}
for(ii = 0; i < N; i++) for(jj = 0; jj < M; jj++) {
vis[ii][jj] = false;
cnt[arr[ii][jj]]++;
}
}
int main(){
ericxiao;
cin >> N >> M;
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
cin >> arr[i][j];
lisan.push_back(arr[i][j]);
}
}
sort(lisan.begin(), lisan.end());
sz = unique(lisan.begin(), lisan.end()) - lisan.begin();
pos.resize(sz + 5);
cnt.resize(sz + 5);
for(int i = 0; i < N; i++) for(int j = 0; j < M; j++){
arr[i][j] = lower_bound(lisan.begin(), lisan.begin() + sz, arr[i][j]) - lisan.begin();
cnt[arr[i][j]]++;
pos[arr[i][j]].emplace_back(i, j);
}
int ans = lisan[sz - 1] - lisan[0];
for(int jizz = 0; jizz < 4; jizz++){
//cout << "in jizz = " << jizz << endl;
int mx = -INF, mn = INF, f = 0, e = sz - 1;
for(int i = 0; i < sz; i++){
//take all of them
//
//cout << "i = " << i << ", = " << lisan[i] << endl;
for(auto &[y, x] : pos[i]){
take(y, x, mx, mn, jizz);
}
/*
for(int y = 0; y < N; y++) for(int x = 0; x < M; x++){
cout << vis[y][x] << " \n"[x == M - 1];
}
*/
while(f < sz - 1 && !cnt[f]) f++;
while(e && !cnt[e]) e--;
if(f == sz || !e) break;
ans = min(ans, max(lisan[e] - lisan[f], lisan[mx] - lisan[mn]));
//cout << "ans = " << ans << endl;
}
rot();
}
cout << ans << endl;
}
Compilation message
joioi.cpp:1: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
1 | #pragma gcc optimize ("Ofast")
|
joioi.cpp: In function 'void rot()':
joioi.cpp:60:14: error: 'i' was not declared in this scope; did you mean 'ii'?
60 | for(ii = 0; i < sz; i++){
| ^
| ii
joioi.cpp:63:14: error: 'i' was not declared in this scope; did you mean 'ii'?
63 | for(ii = 0; i < N; i++) for(jj = 0; jj < M; jj++) {
| ^
| ii