#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
template <class T> void pv(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) cerr << *i << " "; cerr << "]\n";}
void _print() {cerr << "]\n";}
template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
const int mx = 2e3+5;
int n,m,a[mx][mx], lma[mx][mx], lmi[mx][mx], rma[mx][mx], rmi[mx][mx], small = INT_MAX, large = INT_MIN, ans;
void recalc() {
for (int q = 0; q < n; q++) {
lma[q][0] = a[q][0];
lmi[q][0] = a[q][0];
rma[q][m-1] = a[q][m-1];
rmi[q][m-1] = a[q][m-1];
for (int w = 1; w < m; w++) {
lma[q][w] = max(lma[q][w-1], a[q][w]);
lmi[q][w] = min(lmi[q][w-1], a[q][w]);
}
for (int w = m-2; w >= 0; w--) {
rma[q][w] = max(rma[q][w+1], a[q][w]);
rmi[q][w] = min(rmi[q][w+1], a[q][w]);
}
}
}
bool check(int x) {
int lasma = 0, lasmi = 0;
bool okma = true, okmi = true;
for (int q = 0; q < n; q++) {
for (int w = lasma; w < m; w++) {
if (w < m-1) {
if (lma[q][w]-small <= x && large-rmi[q][w+1] <= x) {
lasma = w;
break;
}
} else {
if (lma[q][w]-small <= x) lasma = w;
else okma = false;
}
}
for (int w = lasmi; w < m; w++) {
if (w < m-1) {
if (large-lmi[q][w] <= x && rma[q][w+1]-small <= x) {
lasmi = w;
break;
}
} else {
if (large-lmi[q][w] <= x) lasmi = w;
else okmi = false;
}
}
}
if (okmi || okma) return true;
return false;
}
const int inf = 1e9;
void work() {
int lo = -1, hi = inf+1;
while (hi - lo > 1) {
int mid = (lo+hi)/2;
if(check(mid)) hi = mid;
else lo = mid;
}
ans = min(ans, hi);
}
void printa() {
cout << "BEGIN\n";
for (int q = 0; q < n; q++) {
for (int w = 0; w < m; w++) {
cout << a[q][w] << " ";
}
cout << "\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int q = 0; q < n; q++) {
for (int w = 0; w < m; w++) {
cin >> a[q][w];
small = min(small, a[q][w]);
large = max(large, a[q][w]);
}
}
ans = large-small;
recalc();
work();
int tmp[mx][mx];
for (int q = 0; q < n; q++) {
for (int w = 0; w < m; w++) {
tmp[w][q] = a[q][w];
}
}
swap(n,m);
for (int q = 0; q < n; q++) {
for (int w = 0; w < m; w++) {
a[q][w] = tmp[q][w];
}
}
recalc();
work();
for (int q = 0; q < n; q++) {
for (int w = 0; w < m; w++) {
tmp[m-1-w][q] = a[q][w];
}
}
swap(n,m);
for (int q = 0; q < n; q++) {
for (int w = 0; w < m; w++) {
a[q][w] = tmp[q][w];
}
}
recalc();
work();
for (int q = 0; q < n; q++) {
for (int w = 0; w < m; w++) {
tmp[w][q] = a[q][w];
}
}
swap(n,m);
for (int q = 0; q < n; q++) {
for (int w = 0; w < m; w++) {
a[q][w] = tmp[q][w];
}
}
recalc();
work();
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
624 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
624 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
4 ms |
5356 KB |
Output is correct |
16 |
Correct |
10 ms |
6252 KB |
Output is correct |
17 |
Correct |
9 ms |
6124 KB |
Output is correct |
18 |
Correct |
10 ms |
6124 KB |
Output is correct |
19 |
Correct |
10 ms |
6124 KB |
Output is correct |
20 |
Correct |
9 ms |
6380 KB |
Output is correct |
21 |
Correct |
12 ms |
6508 KB |
Output is correct |
22 |
Correct |
12 ms |
6528 KB |
Output is correct |
23 |
Correct |
11 ms |
6528 KB |
Output is correct |
24 |
Correct |
11 ms |
6508 KB |
Output is correct |
25 |
Correct |
12 ms |
6508 KB |
Output is correct |
26 |
Correct |
14 ms |
6508 KB |
Output is correct |
27 |
Correct |
12 ms |
6508 KB |
Output is correct |
28 |
Correct |
12 ms |
6508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
624 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
4 ms |
5356 KB |
Output is correct |
16 |
Correct |
10 ms |
6252 KB |
Output is correct |
17 |
Correct |
9 ms |
6124 KB |
Output is correct |
18 |
Correct |
10 ms |
6124 KB |
Output is correct |
19 |
Correct |
10 ms |
6124 KB |
Output is correct |
20 |
Correct |
9 ms |
6380 KB |
Output is correct |
21 |
Correct |
12 ms |
6508 KB |
Output is correct |
22 |
Correct |
12 ms |
6528 KB |
Output is correct |
23 |
Correct |
11 ms |
6528 KB |
Output is correct |
24 |
Correct |
11 ms |
6508 KB |
Output is correct |
25 |
Correct |
12 ms |
6508 KB |
Output is correct |
26 |
Correct |
14 ms |
6508 KB |
Output is correct |
27 |
Correct |
12 ms |
6508 KB |
Output is correct |
28 |
Correct |
12 ms |
6508 KB |
Output is correct |
29 |
Correct |
750 ms |
116704 KB |
Output is correct |
30 |
Correct |
746 ms |
116204 KB |
Output is correct |
31 |
Correct |
818 ms |
117736 KB |
Output is correct |
32 |
Correct |
802 ms |
117848 KB |
Output is correct |
33 |
Correct |
718 ms |
114028 KB |
Output is correct |
34 |
Correct |
814 ms |
118000 KB |
Output is correct |
35 |
Correct |
1000 ms |
133484 KB |
Output is correct |
36 |
Correct |
865 ms |
127724 KB |
Output is correct |
37 |
Correct |
931 ms |
133480 KB |
Output is correct |