// Om Namah Shivaya
#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;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a, b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a, b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
https://codeforces.com/blog/entry/50413
(for 60 points):
stress tested to identify the bug: https://oj.uz/submission/767335
didnt set the bounds for the binary search properly (wrote n instead of m)
*/
const int MOD = 1e9 + 7;
const int N = 2e3 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
ll n,m;
ll a[N][N], pmn[N][N], pmx[N][N], smn[N][N], smx[N][N];
void solve(int test_case)
{
cin >> n >> m;
rep1(i,n) rep1(j,m) cin >> a[i][j];
memset(pmn,0x10,sizeof pmn);
memset(smn,0x10,sizeof smn);
memset(pmx,-0x10,sizeof pmx);
memset(smx,-0x10,sizeof smx);
rep1(i,n){
rep1(j,m){
pmn[i][j] = min(pmn[i][j-1], a[i][j]);
pmx[i][j] = max(pmx[i][j-1], a[i][j]);
}
rev(j,m,1){
smn[i][j] = min(smn[i][j+1], a[i][j]);
smx[i][j] = max(smx[i][j+1], a[i][j]);
}
}
ll mn = inf2, mx = -inf2;
rep1(i,n){
rep1(j,m){
amin(mn,a[i][j]);
amax(mx,a[i][j]);
}
}
auto ok = [&](ll k){
// min territory, decreasing
{
ll prev_len = inf2;
bool flag = true;
rep1(i,n){
ll len = 0;
rep1(j,m){
if(a[i][j] <= mn+k){
len++;
}
else{
break;
}
}
amin(len,prev_len);
prev_len = len;
for(int j = len+1; j <= m; ++j){
if(a[i][j] < mx-k){
flag = false;
}
}
}
if(flag) return true;
}
// min territory, decreasing
{
ll prev_len = inf2;
bool flag = true;
rev(i,n,1){
ll len = 0;
rep1(j,m){
if(a[i][j] <= mn+k){
len++;
}
else{
break;
}
}
amin(len,prev_len);
prev_len = len;
for(int j = len+1; j <= m; ++j){
if(a[i][j] < mx-k){
flag = false;
}
}
}
if(flag) return true;
}
return false;
};
ll l = 0, r = inf1;
ll ans = -1;
while(l <= r){
ll mid = (l+r) >> 1;
if(ok(mid)){
ans = mid;
r = mid-1;
}
else{
l = mid+1;
}
}
cout << ans << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
126164 KB |
Output is correct |
2 |
Incorrect |
38 ms |
126168 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
126164 KB |
Output is correct |
2 |
Incorrect |
38 ms |
126168 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
126164 KB |
Output is correct |
2 |
Incorrect |
38 ms |
126168 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |