#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <unordered_map>
using namespace std;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
typedef int ll;
typedef long double ld;
const ll SZ = 2050, INF = 1e9 + 10;
ll grid[SZ][SZ], pt[SZ];
vector<tuple<ll, ll, ll>> vec;
ll n, m, mx;
pair<ll, ll> pos;
void inc(ll ht) {
if (pt[ht] == INF) return;
while (pt[ht + 1] < pt[ht] + 1) {
inc(ht + 1);
if (mx == INF) return;
}
if (make_pair(ht, pt[ht]) == pos) {
mx = INF;
pt[ht] = INF;
return;
}
mx = max(mx, grid[ht][pt[ht]]);
pt[ht]++;
}
ll find() {
for (int i = 0; i < n; i++) pt[i] = 0;
pt[n] = INF;
mx = get<0>(vec[0]);
pos = { get<1>(vec.back()), get<2>(vec.back()) };
ll ans = INF, st = get<0>(vec.back());
for (int i = 0; i < vec.size(); i++) {
ll x = get<1>(vec[i]), y = get<2>(vec[i]);
while (pt[x] <= y) {
inc(x);
if (mx == INF) break;
}
if (mx == INF) break;
ans = min(ans, max(mx - get<0>(vec[0]), st - get<0>(vec[i + 1])));
}
return ans;
}
int main() {
fastInp;
cin >> n >> m;
pair<ll, ll> cur = { INF, -INF };
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
cur.first = min(cur.first, grid[i][j]);
cur.second = max(cur.second, grid[i][j]);
}
}
for (int i = 0; i < n; i++) {
pt[i] = 0;
for (int j = 0; j < m; j++) {
vec.push_back({ grid[i][j], i, j });
}
}
sort(vec.begin(), vec.end());
ll ans = (cur.second - cur.first);
ans = min(ans, find());
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ll snd = m - j - 1;
if (j < snd) swap(grid[i][j], grid[i][snd]);
}
}
for (auto& c : vec) get<2>(c) = m - get<2>(c) - 1;
ans = min(ans, find());
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ll snd = n - i - 1;
if (i < snd) swap(grid[i][j], grid[snd][j]);
}
}
for (auto& c : vec) get<1>(c) = n - get<1>(c) - 1;
ans = min(ans, find());
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ll snd = m - j - 1;
if (j < snd) swap(grid[i][j], grid[i][snd]);
}
}
for (auto& c : vec) get<2>(c) = m - get<2>(c) - 1;
ans = min(ans, find());
cout << ans;
return 0;
}
Compilation message
joioi.cpp: In function 'll find()':
joioi.cpp:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 0; i < vec.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
9 ms |
1916 KB |
Output is correct |
17 |
Correct |
10 ms |
1992 KB |
Output is correct |
18 |
Correct |
9 ms |
1992 KB |
Output is correct |
19 |
Correct |
10 ms |
1992 KB |
Output is correct |
20 |
Correct |
9 ms |
1864 KB |
Output is correct |
21 |
Correct |
10 ms |
1992 KB |
Output is correct |
22 |
Correct |
10 ms |
1960 KB |
Output is correct |
23 |
Correct |
10 ms |
1936 KB |
Output is correct |
24 |
Correct |
9 ms |
1864 KB |
Output is correct |
25 |
Correct |
11 ms |
1992 KB |
Output is correct |
26 |
Correct |
11 ms |
1992 KB |
Output is correct |
27 |
Correct |
11 ms |
1956 KB |
Output is correct |
28 |
Correct |
14 ms |
1992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
9 ms |
1916 KB |
Output is correct |
17 |
Correct |
10 ms |
1992 KB |
Output is correct |
18 |
Correct |
9 ms |
1992 KB |
Output is correct |
19 |
Correct |
10 ms |
1992 KB |
Output is correct |
20 |
Correct |
9 ms |
1864 KB |
Output is correct |
21 |
Correct |
10 ms |
1992 KB |
Output is correct |
22 |
Correct |
10 ms |
1960 KB |
Output is correct |
23 |
Correct |
10 ms |
1936 KB |
Output is correct |
24 |
Correct |
9 ms |
1864 KB |
Output is correct |
25 |
Correct |
11 ms |
1992 KB |
Output is correct |
26 |
Correct |
11 ms |
1992 KB |
Output is correct |
27 |
Correct |
11 ms |
1956 KB |
Output is correct |
28 |
Correct |
14 ms |
1992 KB |
Output is correct |
29 |
Correct |
1127 ms |
64900 KB |
Output is correct |
30 |
Correct |
1030 ms |
87196 KB |
Output is correct |
31 |
Correct |
1115 ms |
88844 KB |
Output is correct |
32 |
Correct |
1109 ms |
88848 KB |
Output is correct |
33 |
Correct |
979 ms |
83788 KB |
Output is correct |
34 |
Correct |
1116 ms |
89080 KB |
Output is correct |
35 |
Correct |
1168 ms |
104508 KB |
Output is correct |
36 |
Correct |
959 ms |
99268 KB |
Output is correct |
37 |
Correct |
1142 ms |
104552 KB |
Output is correct |