/* be name khoda */
// #define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <vector>
#include <fstream>
#include <set>
#include <map>
using namespace std;
#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif
typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;
#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define forirto(i, n, e) for (ll i = (n) - 1; i >= e; --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))
#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl
const ll MOD = 1000000007;
const ll INF = 1000000000;
const long long BIG = 1446803456761533460LL;
#define XL (x << 1)
#define XR (XL | 1)
#define MD ((l + r) >> 1)
#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD
// -----------------------------------------------------------------------
const ll maxn = 2010;
typedef array<array<ll, maxn>, maxn> Grid;
ll n, m, mn, mx;
Grid A;
bool check(ll k) {
ll h = m;
fori (i, n) {
ll j = 0;
while (j < h && A[i][j] - mn <= k) ++j;
h = j;
forifrom (j, h, m) {
if (mx - A[i][j] > k) return false;
}
}
return true;
}
ll solve() {
ll l = -1, r = INF;
while (r - l > 1) {
ll mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid;
}
return r;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
mn = INF, mx = -INF;
fori (i, n) fori (j, m) cin >> A[i][j], smin(mn, A[i][j]), smax(mx, A[i][j]);
ll ans = solve();
reverse(A.begin(), A.begin() + n);
smin(ans, solve());
fori (i, n) reverse(A[i].begin(), A[i].begin() + m);
smin(ans, solve());
reverse(A.begin(), A.begin() + n);
smin(ans, solve());
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
9 ms |
1920 KB |
Output is correct |
17 |
Correct |
11 ms |
2176 KB |
Output is correct |
18 |
Correct |
12 ms |
2136 KB |
Output is correct |
19 |
Correct |
13 ms |
2304 KB |
Output is correct |
20 |
Correct |
10 ms |
1920 KB |
Output is correct |
21 |
Correct |
11 ms |
2304 KB |
Output is correct |
22 |
Correct |
15 ms |
2304 KB |
Output is correct |
23 |
Correct |
12 ms |
2304 KB |
Output is correct |
24 |
Correct |
10 ms |
2048 KB |
Output is correct |
25 |
Correct |
12 ms |
2304 KB |
Output is correct |
26 |
Correct |
11 ms |
2304 KB |
Output is correct |
27 |
Correct |
14 ms |
2336 KB |
Output is correct |
28 |
Correct |
11 ms |
2304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
9 ms |
1920 KB |
Output is correct |
17 |
Correct |
11 ms |
2176 KB |
Output is correct |
18 |
Correct |
12 ms |
2136 KB |
Output is correct |
19 |
Correct |
13 ms |
2304 KB |
Output is correct |
20 |
Correct |
10 ms |
1920 KB |
Output is correct |
21 |
Correct |
11 ms |
2304 KB |
Output is correct |
22 |
Correct |
15 ms |
2304 KB |
Output is correct |
23 |
Correct |
12 ms |
2304 KB |
Output is correct |
24 |
Correct |
10 ms |
2048 KB |
Output is correct |
25 |
Correct |
12 ms |
2304 KB |
Output is correct |
26 |
Correct |
11 ms |
2304 KB |
Output is correct |
27 |
Correct |
14 ms |
2336 KB |
Output is correct |
28 |
Correct |
11 ms |
2304 KB |
Output is correct |
29 |
Correct |
816 ms |
19236 KB |
Output is correct |
30 |
Correct |
802 ms |
19960 KB |
Output is correct |
31 |
Correct |
878 ms |
20088 KB |
Output is correct |
32 |
Correct |
868 ms |
20228 KB |
Output is correct |
33 |
Correct |
752 ms |
17756 KB |
Output is correct |
34 |
Correct |
883 ms |
20100 KB |
Output is correct |
35 |
Correct |
927 ms |
22904 KB |
Output is correct |
36 |
Correct |
810 ms |
22136 KB |
Output is correct |
37 |
Correct |
845 ms |
23032 KB |
Output is correct |