#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;
vector<int> lisan, cnt;
vector<vector<pii>> pos;
bool vis[maxN][maxN];
void take(int y, int x, int &mx, int &mn, int d){
if(d == 0){
for(int i = y; i >= 0 && !vis[i][x]; i--){
for(int j = x; j >= 0 && !vis[i][j]; j--){
vis[i][j] = true;
cnt[arr[i][j]]--;
mx = max(mx, arr[i][j]);
mn = min(mn, arr[i][j]);
}
}
} else if(d == 1){
for(int i = y; i < N && !vis[i][x]; i++){
for(int j = x; j >= 0 && !vis[i][j]; j--){
vis[i][j] = true;
cnt[arr[i][j]]--;
mx = max(mx, arr[i][j]);
mn = min(mn, arr[i][j]);
}
}
} else if(d == 2){
for(int i = y; i < N && !vis[i][x]; i++){
for(int j = x; j < M && !vis[i][j]; j++){
vis[i][j] = true;
cnt[arr[i][j]]--;
mx = max(mx, arr[i][j]);
mn = min(mn, arr[i][j]);
}
}
} else if(d == 3){
for(int i = y; i >= 0 && !vis[i][x]; i--){
for(int j = x; j < M && !vis[i][j]; j++){
vis[i][j] = true;
cnt[arr[i][j]]--;
mx = max(mx, arr[i][j]);
mn = min(mn, arr[i][j]);
}
}
}
}
inline void rot(){
for(int i = 0; i < sz; i++){
cnt[i] = 0;
}
for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) {
vis[i][j] = false;
cnt[arr[i][j]]++;
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
8 ms |
2288 KB |
Output is correct |
17 |
Correct |
21 ms |
4208 KB |
Output is correct |
18 |
Correct |
21 ms |
4208 KB |
Output is correct |
19 |
Correct |
21 ms |
4208 KB |
Output is correct |
20 |
Correct |
18 ms |
3824 KB |
Output is correct |
21 |
Correct |
23 ms |
4336 KB |
Output is correct |
22 |
Correct |
21 ms |
4208 KB |
Output is correct |
23 |
Correct |
22 ms |
4336 KB |
Output is correct |
24 |
Correct |
19 ms |
3824 KB |
Output is correct |
25 |
Correct |
22 ms |
4348 KB |
Output is correct |
26 |
Correct |
22 ms |
4336 KB |
Output is correct |
27 |
Correct |
22 ms |
4336 KB |
Output is correct |
28 |
Correct |
22 ms |
4336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
8 ms |
2288 KB |
Output is correct |
17 |
Correct |
21 ms |
4208 KB |
Output is correct |
18 |
Correct |
21 ms |
4208 KB |
Output is correct |
19 |
Correct |
21 ms |
4208 KB |
Output is correct |
20 |
Correct |
18 ms |
3824 KB |
Output is correct |
21 |
Correct |
23 ms |
4336 KB |
Output is correct |
22 |
Correct |
21 ms |
4208 KB |
Output is correct |
23 |
Correct |
22 ms |
4336 KB |
Output is correct |
24 |
Correct |
19 ms |
3824 KB |
Output is correct |
25 |
Correct |
22 ms |
4348 KB |
Output is correct |
26 |
Correct |
22 ms |
4336 KB |
Output is correct |
27 |
Correct |
22 ms |
4336 KB |
Output is correct |
28 |
Correct |
22 ms |
4336 KB |
Output is correct |
29 |
Correct |
1903 ms |
90552 KB |
Output is correct |
30 |
Correct |
1878 ms |
86584 KB |
Output is correct |
31 |
Correct |
1901 ms |
87748 KB |
Output is correct |
32 |
Correct |
2146 ms |
95928 KB |
Output is correct |
33 |
Correct |
1669 ms |
82508 KB |
Output is correct |
34 |
Correct |
2007 ms |
87644 KB |
Output is correct |
35 |
Runtime error |
3460 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
36 |
Halted |
0 ms |
0 KB |
- |