#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) (((a) + (b) - 1) / (b))
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
bool check(int h, int w, vector<vector<int>>& a, int d){
vector<int> pos(h, -1);
for(int i = 0; i < h; i++){
for(int j = 0; j + 1 < w; j++){
if(abs(a[i][j] - a[i][j + 1]) <= d) continue;
if(pos[i] != -1) return false;
pos[i] = j;
}
}
vector<int> suf(h, w - 1);
for(int i = h - 1; i >= 0; i--){
if(i < h - 1) suf[i] = suf[i + 1];
if(pos[i] != -1) suf[i] = min(suf[i], pos[i]);
}
int pre = 0;
for(int i = 0; i < h - 1; i++){
pre = max(pre, pos[i]);
int tmp = pos[i + 1];
for(int j = 0; j < w; j++){
if(abs(a[i][j] - a[i + 1][j]) <= d) continue;
if(j <= pre || j > suf[i + 1]) return false;
if(pos[i + 1] == -1) tmp = j;
}
pos[i + 1] = tmp;
}
return true;
}
int calc(int h, int w, vector<vector<int>>& a){
int l = 0, r = 1000000000;
while(l < r){
int m = (l + r) / 2;
if(check(h, w, a, m)) r = m;
else l = m + 1;
}
return l;
}
int main(){
StarBurstStream
int h, w;
cin >> h >> w;
vector<vector<int>> a(h, vector<int>(w));
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
cin >> a[i][j];
}
}
int t1 = calc(h, w, a);
reverse(iter(a));
int t2 = calc(h, w, a);
cout << min(t1, t2) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |