#include <iostream>
#include <iomanip>
#include <algorithm>
#include <bitset>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <time.h>
#include <random>
#include <string>
#include <cassert>
#include <vector>
#include <ostream>
#include <istream>
#include <stack>
#include <deque>
#include <queue>
#include <functional>
#include <chrono>
#include <stack>
#include <limits>
using namespace std;
#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define ld long double
istream& operator>> (istream& in, pii& b) {
in >> b.first >> b.second;
return in;
}
ostream& operator<< (ostream& out, const pii& b) {
out << "{" << b.first << ", " << b.second << "}";
return out;
}
template<typename T> ostream& operator<< (ostream& out, const vector<T>& a) {
for (auto k : a) out << k << " ";
return out;
}
template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
#ifdef LOCAL
#define dbg(x) cout << #x << " : " << (x) << endl;
const long long INF = 1e18;
// const long long mod = 2600000069;
// const long long p = 10;
#else
#define dbg(x) 57
const long long INF = 1e18;
// const long long mod = 2600000069;
// const long long p = 179;
#endif
const ld PI = 4 * atan(1);
#define time clock() / (double) CLOCKS_PER_SEC
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,sse3,sse4")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
// #pragma GCC target("avx2")
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
#ifdef LOCAL
const int N = 550;
#else
const int N = 2010;
#endif
int n, m;
int a[N][N];
int mn = INF, mx = -INF;
bool check(int d) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] - mn > d && mx - a[i][j] > d) {
return 0;
}
}
}
int pos;
bool ok, seen;
// mn in upper left
ok = 1;
pos = -1;
for (int i = n - 1; i >= 0; i--) {
seen = 0;
for (int j = 0; j < m; j++) {
if (a[i][j] - mn <= d && mx - a[i][j] <= d) continue;
if (a[i][j] - mn <= d) {
if (chkmax(pos, j) && seen) {
ok = 0;
break;
}
} else {
if (j <= pos) {
ok = 0;
break;
}
seen = 1;
}
}
if (!ok) break;
}
if (ok) return 1;
// mn in lower left
ok = 1;
pos = -1;
for (int i = 0; i < n; i++) {
seen = 0;
for (int j = 0; j < m; j++) {
if (a[i][j] - mn <= d && mx - a[i][j] <= d) continue;
if (a[i][j] - mn <= d) {
if (chkmax(pos, j) && seen) {
ok = 0;
break;
}
} else {
if (j <= pos) {
ok = 0;
break;
}
seen = 1;
}
}
if (!ok) break;
}
if (ok) return 1;
// mx in upper left
ok = 1;
pos = -1;
for (int i = n - 1; i >= 0; i--) {
seen = 0;
for (int j = 0; j < m; j++) {
if (a[i][j] - mn <= d && mx - a[i][j] <= d) continue;
if (mx - a[i][j] <= d) {
if (chkmax(pos, j) && seen) {
ok = 0;
break;
}
} else {
if (j <= pos) {
ok = 0;
break;
}
}
}
if (!ok) {
break;
}
}
if (ok) return 1;
// mx in lower left
ok = 1;
pos = -1;
for (int i = 0; i < n; i++) {
seen = 0;
for (int j = 0; j < m; j++) {
if (a[i][j] - mn <= d && mx - a[i][j] <= d) continue;
if (mx - a[i][j] <= d) {
if (chkmax(pos, j) && seen) {
ok = 0;
break;
}
} else {
if (j <= pos) {
ok = 0;
break;
}
}
}
if (!ok) break;
}
return ok;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
int sz = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
chkmin(mn, a[i][j]);
chkmax(mx, a[i][j]);
}
}
int l = -1, r = mx - mn;
while (r - l > 1) {
int mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid;
}
cout << r << "\n";
}
/*
4 4
1 12 6 11
11 10 2 14
10 1 9 20
4 17 19 10
-> 11
8 6
23 23 10 11 16 21
15 26 19 28 19 20
25 26 28 16 15 11
11 8 19 11 15 24
14 19 15 14 24 11
10 8 11 7 6 14
23 5 19 23 17 17
18 11 21 14 20 16
-> 18
*/
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:198:9: warning: unused variable 'sz' [-Wunused-variable]
198 | int sz = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
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 |
364 KB |
Output is correct |
16 |
Correct |
4 ms |
1388 KB |
Output is correct |
17 |
Correct |
7 ms |
1516 KB |
Output is correct |
18 |
Correct |
6 ms |
1772 KB |
Output is correct |
19 |
Correct |
9 ms |
1772 KB |
Output is correct |
20 |
Correct |
9 ms |
1516 KB |
Output is correct |
21 |
Correct |
10 ms |
1900 KB |
Output is correct |
22 |
Correct |
8 ms |
1900 KB |
Output is correct |
23 |
Correct |
12 ms |
1900 KB |
Output is correct |
24 |
Correct |
6 ms |
1644 KB |
Output is correct |
25 |
Correct |
14 ms |
1900 KB |
Output is correct |
26 |
Correct |
9 ms |
1900 KB |
Output is correct |
27 |
Correct |
10 ms |
1900 KB |
Output is correct |
28 |
Correct |
10 ms |
1900 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 |
364 KB |
Output is correct |
16 |
Correct |
4 ms |
1388 KB |
Output is correct |
17 |
Correct |
7 ms |
1516 KB |
Output is correct |
18 |
Correct |
6 ms |
1772 KB |
Output is correct |
19 |
Correct |
9 ms |
1772 KB |
Output is correct |
20 |
Correct |
9 ms |
1516 KB |
Output is correct |
21 |
Correct |
10 ms |
1900 KB |
Output is correct |
22 |
Correct |
8 ms |
1900 KB |
Output is correct |
23 |
Correct |
12 ms |
1900 KB |
Output is correct |
24 |
Correct |
6 ms |
1644 KB |
Output is correct |
25 |
Correct |
14 ms |
1900 KB |
Output is correct |
26 |
Correct |
9 ms |
1900 KB |
Output is correct |
27 |
Correct |
10 ms |
1900 KB |
Output is correct |
28 |
Correct |
10 ms |
1900 KB |
Output is correct |
29 |
Correct |
559 ms |
52332 KB |
Output is correct |
30 |
Correct |
632 ms |
53484 KB |
Output is correct |
31 |
Correct |
819 ms |
55020 KB |
Output is correct |
32 |
Correct |
549 ms |
55020 KB |
Output is correct |
33 |
Correct |
385 ms |
47980 KB |
Output is correct |
34 |
Correct |
698 ms |
55148 KB |
Output is correct |
35 |
Correct |
802 ms |
70764 KB |
Output is correct |
36 |
Correct |
598 ms |
65516 KB |
Output is correct |
37 |
Correct |
1216 ms |
70896 KB |
Output is correct |