#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template <class T, class ...U>
void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template <class T>
void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l) == r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
template <class T>
bool chmin(T &a, T b) { return b < a ? (a = b, true) : false; }
template <class T>
bool chmax(T &a, T b) { return a < b ? (a = b, true) : false; }
const int MAX_N = 2010, inf = 1e9;
//#define int ll
int n, m, a[MAX_N][MAX_N], res = inf;
int pfmx[MAX_N][MAX_N];
vector<pair<int,int>> id;
void init() {
id.reserve(n * m);
for (int i = 1;i <= n;++i)
for (int j = 1;j <= m;++j)
id.pb(i, j);
sort(AI(id), [&](auto x, auto y) {
return a[x.first][x.second] < a[y.first][y.second];
});
}
void update() {
for (int i = 1;i <= n;++i)
for (int j = 1;j <= m;++j) {
pfmx[i][j] = max({pfmx[i-1][j], pfmx[i][j-1], a[i][j]});
}
auto get = [&](pair<int,int> p) {
return a[p.first][p.second];
};
for (int i = 0, j = 0;i+1 < id.size();++i) {
auto valid = [&](pair<int,int> p) {
return pfmx[p.first][p.second] <= get(id[i]);
};
while (j <= i && valid(id[j]))
++j;
if (valid(id[0]) && valid(id[i]))
chmin(res, max(get(id[i]) - get(id[0]), get(id.back()) - get(id[j])));
}
chmin(res, get(id.back()) - get(id.front()));
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1;i <= n;++i)
for (int j = 1;j <= m;++j)
cin >> a[i][j];
init();
update();
for (int l = 1, r = n;l < r;++l, --r)
swap(a[l], a[r]);
for (auto &[x, y] : id)
x = n - x + 1;
update();
for (int i = 1;i <= n;++i) {
for (int l = 1, r = m;l < r;++l, --r)
swap(a[i][l], a[i][r]);
}
for (auto &[x, y] : id)
y = m - y + 1;
update();
for (int l = 1, r = n;l < r;++l, --r)
swap(a[l], a[r]);
for (auto &[x, y] : id)
x = n - x + 1;
update();
cout << res << '\n';
}
Compilation message
joioi.cpp: In function 'void update()':
joioi.cpp:48:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0, j = 0;i+1 < id.size();++i) {
| ~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
8 ms |
3180 KB |
Output is correct |
17 |
Correct |
13 ms |
3180 KB |
Output is correct |
18 |
Correct |
13 ms |
3180 KB |
Output is correct |
19 |
Correct |
12 ms |
3180 KB |
Output is correct |
20 |
Correct |
11 ms |
2924 KB |
Output is correct |
21 |
Correct |
13 ms |
3180 KB |
Output is correct |
22 |
Correct |
13 ms |
3180 KB |
Output is correct |
23 |
Correct |
13 ms |
3180 KB |
Output is correct |
24 |
Correct |
11 ms |
2924 KB |
Output is correct |
25 |
Correct |
13 ms |
3180 KB |
Output is correct |
26 |
Correct |
16 ms |
3180 KB |
Output is correct |
27 |
Correct |
13 ms |
3180 KB |
Output is correct |
28 |
Correct |
13 ms |
3180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
8 ms |
3180 KB |
Output is correct |
17 |
Correct |
13 ms |
3180 KB |
Output is correct |
18 |
Correct |
13 ms |
3180 KB |
Output is correct |
19 |
Correct |
12 ms |
3180 KB |
Output is correct |
20 |
Correct |
11 ms |
2924 KB |
Output is correct |
21 |
Correct |
13 ms |
3180 KB |
Output is correct |
22 |
Correct |
13 ms |
3180 KB |
Output is correct |
23 |
Correct |
13 ms |
3180 KB |
Output is correct |
24 |
Correct |
11 ms |
2924 KB |
Output is correct |
25 |
Correct |
13 ms |
3180 KB |
Output is correct |
26 |
Correct |
16 ms |
3180 KB |
Output is correct |
27 |
Correct |
13 ms |
3180 KB |
Output is correct |
28 |
Correct |
13 ms |
3180 KB |
Output is correct |
29 |
Correct |
1885 ms |
60068 KB |
Output is correct |
30 |
Correct |
1797 ms |
60792 KB |
Output is correct |
31 |
Correct |
1921 ms |
63084 KB |
Output is correct |
32 |
Correct |
2011 ms |
63084 KB |
Output is correct |
33 |
Correct |
1563 ms |
55276 KB |
Output is correct |
34 |
Correct |
1960 ms |
63084 KB |
Output is correct |
35 |
Correct |
2112 ms |
63468 KB |
Output is correct |
36 |
Correct |
1686 ms |
59116 KB |
Output is correct |
37 |
Correct |
2093 ms |
63084 KB |
Output is correct |