#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
//#define int long long
const int nmax = 2e3 + 5;
const int lg = 18;
const int base = 311;
const int M = 5e6;
const ll oo = 1e9 + 1;
const int mod = 987654321;
#define fi first
#define se second
#define endl "\n"
#define debug(a, m) for(int j = 1; j <= n; ++j) cout << a[j] << ' ';cout << endl;
#define pii pair<ll, int>
using namespace std;
int n, m, a[nmax][nmax], mx = -1;
int pos[nmax], pre[nmax][nmax][2], suf[nmax][nmax][2];
int tmp[nmax][nmax];
bool check(int mid){
for(int k = 1; k <= 4; ++k){
for(int i = 1; i <= n; ++i){
pre[i][0][0] = suf[i][m + 1][0] = oo;
pre[i][0][1] = suf[i][m + 1][1] = -oo;
for(int j = 1; j <= m; ++j){
pre[i][j][0] = min(pre[i][j - 1][0], a[i][j]);
pre[i][j][1] = max(pre[i][j - 1][1], a[i][j]);
}
for(int j = m; j >= 1; --j){
suf[i][j][0] = min(suf[i][j + 1][0], a[i][j]);
suf[i][j][1] = max(suf[i][j + 1][1], a[i][j]);
}
}
for(int i = 1; i <= n; ++i){
pos[i] = -1;
for(int j = 1; j <= m; ++j){
if(mx - mid > a[i][j]){
pos[i] = j;
}
}
// cout << pos[i] << ' ';
}
// return 0;
int ma_1 = -oo, mi_1 = oo, ma_2 = -oo, mi_2 = oo, idx = 0;
for(int i = 1; i <= n; ++i){
idx = max(idx, pos[i]);
ma_1 = max(ma_1, pre[i][idx][1]);
ma_2 = max(ma_2, suf[i][idx + 1][1]);
mi_1 = min(mi_1, pre[i][idx][0]);
mi_2 = min(mi_2, suf[i][idx + 1][0]);
}
if(ma_1 - mi_1 <= mid && ma_2 - mi_2 <= mid) return 1;
ma_1 = -oo, mi_1 = oo, ma_2 = -oo, mi_2 = oo, idx = 0;
for(int i = n; i >= 1; --i){
idx = max(idx, pos[i]);
ma_1 = max(ma_1, pre[i][idx][1]);
ma_2 = max(ma_2, suf[i][idx + 1][1]);
mi_1 = min(mi_1, pre[i][idx][0]);
mi_2 = min(mi_2, suf[i][idx + 1][0]);
}
// cout <<ma_1 << ' ' <<mi_1 << ' ' << ma_2 << ' ' << mi_2 << ' ' << mi;
// return 0;
if(ma_1 - mi_1 <= mid && ma_2 - mi_2 <= mid) return 1;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= m; ++j){
tmp[j][n - i + 1] = a[i][j];
}
}
swap(n, m);
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= m; ++j){
a[i][j] = tmp[i][j];
// cout << a[i][j] << ' ';
}
// cout << endl;
}
// return 0;
}
return 0;
}
main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i<= n; ++i){
for(int j = 1; j <= m; ++j){
cin >> a[i][j];
mx = max(mx, a[i][j]);
}
}
// vector<int> nen;
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= m; ++j){
// nen.push_back(mx - a[i][j]);
// }
// }
// sort(nen.begin(), nen.end());
// nen.erase(unique(nen.begin(), nen.end()), nen.end());
// cout <<
//check(11);
int l = 1, r= 1e9, kq = -1;
while(l <= r){
int mid = r + l >> 1;
if(check(mid)){
kq = mid;
r = mid - 1;
}
else l= mid + 1;
}
cout << kq;
// cout << check(11);
// for(int i =1; i <= 100; ++i){
// cout << i << ' ' << check(i) << endl;
// }
}
/*
4 4
1 12 6 11
11 10 2 14
10 1 9 20
4 17 19 10
*/
Compilation message
joioi.cpp:89:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
89 | main(){
| ^~~~
joioi.cpp: In function 'int main()':
joioi.cpp:114:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
114 | int mid = r + l >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
464 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
464 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
3676 KB |
Output is correct |
16 |
Correct |
6 ms |
4592 KB |
Output is correct |
17 |
Correct |
9 ms |
4700 KB |
Output is correct |
18 |
Correct |
10 ms |
4700 KB |
Output is correct |
19 |
Correct |
10 ms |
4900 KB |
Output is correct |
20 |
Correct |
12 ms |
4824 KB |
Output is correct |
21 |
Correct |
15 ms |
5004 KB |
Output is correct |
22 |
Correct |
14 ms |
4956 KB |
Output is correct |
23 |
Correct |
14 ms |
5016 KB |
Output is correct |
24 |
Correct |
12 ms |
4964 KB |
Output is correct |
25 |
Correct |
15 ms |
4956 KB |
Output is correct |
26 |
Correct |
13 ms |
4876 KB |
Output is correct |
27 |
Correct |
15 ms |
4956 KB |
Output is correct |
28 |
Correct |
15 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
464 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
3676 KB |
Output is correct |
16 |
Correct |
6 ms |
4592 KB |
Output is correct |
17 |
Correct |
9 ms |
4700 KB |
Output is correct |
18 |
Correct |
10 ms |
4700 KB |
Output is correct |
19 |
Correct |
10 ms |
4900 KB |
Output is correct |
20 |
Correct |
12 ms |
4824 KB |
Output is correct |
21 |
Correct |
15 ms |
5004 KB |
Output is correct |
22 |
Correct |
14 ms |
4956 KB |
Output is correct |
23 |
Correct |
14 ms |
5016 KB |
Output is correct |
24 |
Correct |
12 ms |
4964 KB |
Output is correct |
25 |
Correct |
15 ms |
4956 KB |
Output is correct |
26 |
Correct |
13 ms |
4876 KB |
Output is correct |
27 |
Correct |
15 ms |
4956 KB |
Output is correct |
28 |
Correct |
15 ms |
4952 KB |
Output is correct |
29 |
Correct |
1230 ms |
116572 KB |
Output is correct |
30 |
Correct |
1162 ms |
116048 KB |
Output is correct |
31 |
Correct |
1334 ms |
117628 KB |
Output is correct |
32 |
Correct |
985 ms |
117744 KB |
Output is correct |
33 |
Correct |
879 ms |
114116 KB |
Output is correct |
34 |
Correct |
1036 ms |
117844 KB |
Output is correct |
35 |
Correct |
1548 ms |
133332 KB |
Output is correct |
36 |
Correct |
1104 ms |
127824 KB |
Output is correct |
37 |
Correct |
1923 ms |
133328 KB |
Output is correct |