// -------------------- Includes -------------------- //
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <array>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <tuple>
#include <utility>
#include <cassert>
#include <assert.h>
#include <climits>
#include <limits.h>
#include <ctime>
#include <time.h>
#include <random>
#include <chrono>
#include <fstream>
using namespace std;
// -------------------- Typedefs -------------------- //
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
// -------------------- Defines -------------------- //
#define pr pair
#define mpr make_pair
#define ff first
#define ss second
#define mset multiset
#define mmap multimap
#define uset unordered_set
#define umap unordered_map
#define umset unordered_multiset
#define ummap unordered_multimap
#define pqueue priority_queue
#define sz(x) (int((x).size()))
#define len(x) (int((x).length()))
#define all(x) (x).begin(), (x).end()
#define clr(x) (x).clear()
#define ft front
#define bk back
#define pf push_front
#define pb push_back
#define popf pop_front
#define popb pop_back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
// -------------------- Constants -------------------- //
const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18 + 5);
const ll MOD = ll(1e9 + 7);
const ll MOD2 = ll(998244353);
// -------------------- Functions -------------------- //
void fastio()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
return;
}
void precision(int x)
{
cout << fixed << setprecision(x);
return;
}
ll gcd0(ll a, ll b)
{
while (b) {
a %= b;
swap(a, b);
}
return a;
}
ll lcm0(ll a, ll b)
{
return a / gcd0(a, b) * b;
}
bool is_prime(ll a)
{
if (a == 1) return false;
for (ll i = 2; i * i <= a; i++) if (a % i == 0) return false;
return true;
}
bool is_square(ll a)
{
ll b = ll(sqrtl(ld(a)));
return (b * b == a);
}
bool is_cube(ll a)
{
ll b = ll(cbrtl(ld(a)));
return (b * b * b == a);
}
int digit_sum(ll a)
{
int sum = 0;
while (a) {
sum += int(a % 10);
a /= 10;
}
return sum;
}
ll binpow(ll a, int b)
{
ll ans = 1;
while (b) {
if (b & 1) ans *= a;
b >>= 1;
a *= a;
}
return ans;
}
ll binpow_mod(ll a, ll b, ll mod)
{
ll ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % mod;
b >>= 1;
a = (a * a) % mod;
}
return ans;
}
ll factorial(int a)
{
ll ans = 1;
for (int i = 2; i <= a; i++) ans *= ll(i);
return ans;
}
ll factorial_mod(int a, ll mod)
{
ll ans = 1;
for (int i = 2; i <= a; i++) ans = (ans * ll(i)) % mod;
return ans;
}
// -------------------- Solution -------------------- //
const int N = 2005, M = 2005;
int a[N][M], b[N][M];
int p[N];
int n, m, mn = MAX;
void rot()
{
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
b[j][n - i + 1] = a[i][j];
}
}
swap(n, m);
for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) a[i][j] = b[i][j];
return;
}
bool check(int h)
{
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) if (a[i][j] - mn > h) break;
p[i] = min(p[i - 1], j - 1);
}
int mn1 = MAX, mx1 = 0;
int mn2 = MAX, mx2 = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= p[i]; j++) {
mn1 = min(mn1, a[i][j]);
mx1 = max(mx1, a[i][j]);
}
for (j = p[i] + 1; j <= m; j++) {
mn2 = min(mn2, a[i][j]);
mx2 = max(mx2, a[i][j]);
}
}
return max(mx1 - mn1, mx2 - mn2) <= h;
}
void solve()
{
int i, j;
cin >> n >> m;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
cin >> a[i][j];
mn = min(mn, a[i][j]);
}
}
p[0] = MAX;
int l = 0, r = int(1e9), mid;
while (l < r) {
mid = (l + r) / 2;
if (check(mid)) r = mid;
else {
rot();
if (check(mid)) r = mid;
else {
rot();
if (check(mid)) r = mid;
else {
rot();
if (check(mid)) r = mid;
else l = mid + 1;
}
}
}
}
cout << l << "\n";
return;
}
void precalc()
{
return;
}
int main()
{
fastio();
precalc();
int tests = 1, tc;
//cin >> tests;
for (tc = 1; tc <= tests; tc++) {
solve();
}
return 0;
}
/*
# # # # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # #
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
5 ms |
6492 KB |
Output is correct |
17 |
Correct |
5 ms |
6612 KB |
Output is correct |
18 |
Correct |
5 ms |
6492 KB |
Output is correct |
19 |
Correct |
5 ms |
6492 KB |
Output is correct |
20 |
Correct |
5 ms |
6492 KB |
Output is correct |
21 |
Correct |
12 ms |
6728 KB |
Output is correct |
22 |
Correct |
12 ms |
6728 KB |
Output is correct |
23 |
Correct |
6 ms |
6492 KB |
Output is correct |
24 |
Correct |
10 ms |
6728 KB |
Output is correct |
25 |
Correct |
9 ms |
6492 KB |
Output is correct |
26 |
Correct |
13 ms |
6612 KB |
Output is correct |
27 |
Correct |
7 ms |
6492 KB |
Output is correct |
28 |
Correct |
10 ms |
6728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
5 ms |
6492 KB |
Output is correct |
17 |
Correct |
5 ms |
6612 KB |
Output is correct |
18 |
Correct |
5 ms |
6492 KB |
Output is correct |
19 |
Correct |
5 ms |
6492 KB |
Output is correct |
20 |
Correct |
5 ms |
6492 KB |
Output is correct |
21 |
Correct |
12 ms |
6728 KB |
Output is correct |
22 |
Correct |
12 ms |
6728 KB |
Output is correct |
23 |
Correct |
6 ms |
6492 KB |
Output is correct |
24 |
Correct |
10 ms |
6728 KB |
Output is correct |
25 |
Correct |
9 ms |
6492 KB |
Output is correct |
26 |
Correct |
13 ms |
6612 KB |
Output is correct |
27 |
Correct |
7 ms |
6492 KB |
Output is correct |
28 |
Correct |
10 ms |
6728 KB |
Output is correct |
29 |
Correct |
547 ms |
31708 KB |
Output is correct |
30 |
Correct |
480 ms |
31692 KB |
Output is correct |
31 |
Correct |
539 ms |
31824 KB |
Output is correct |
32 |
Correct |
486 ms |
31824 KB |
Output is correct |
33 |
Correct |
477 ms |
31692 KB |
Output is correct |
34 |
Correct |
538 ms |
31828 KB |
Output is correct |
35 |
Correct |
833 ms |
31824 KB |
Output is correct |
36 |
Correct |
609 ms |
31788 KB |
Output is correct |
37 |
Correct |
752 ms |
31884 KB |
Output is correct |