#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);
}
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();
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 |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
632 KB |
Output is correct |
7 |
Correct |
1 ms |
620 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 |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
632 KB |
Output is correct |
7 |
Correct |
1 ms |
620 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 |
9 ms |
6400 KB |
Output is correct |
17 |
Correct |
11 ms |
6380 KB |
Output is correct |
18 |
Correct |
10 ms |
6476 KB |
Output is correct |
19 |
Incorrect |
9 ms |
6508 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
632 KB |
Output is correct |
7 |
Correct |
1 ms |
620 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 |
9 ms |
6400 KB |
Output is correct |
17 |
Correct |
11 ms |
6380 KB |
Output is correct |
18 |
Correct |
10 ms |
6476 KB |
Output is correct |
19 |
Incorrect |
9 ms |
6508 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |