This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e3+5;
const int M = 250;
const int mod = 1e9+7;
const ll base = 75;
const int inf = 1e9;
int m, n, t, b[N], d[N], lab[N], c[N];
int k, a[N][N], dp[N], lst[N], fe[N];
ll ans, tong;
vector<int> adj[N], g[N];
vector<int> vi;
string s[N];
bool vis[N];
bool cmp(const int& x, const int& y)
{
return d[x] > d[y];
}
void add(int id, int x)
{
for(; id; id -= id & -id)fe[id] += x;
}
int get(int id)
{
int res = 0;
for(; id <= m; id += id & -id)res += fe[id];
return res;
}
struct IT
{
int n;
vector<int> st;
IT(int _n)
{
n = _n;
st.resize(n*4+4, 0);
}
void build(int id, int l, int r, int pos, int x)
{
if(l == r)
{
st[id] = max(x, st[id]);
return;
}
int mid = (l+r)>>1;
if(pos <= mid)build(id<<1, l, mid, pos, x);
else build(id<<1|1, mid+1, r, pos, x);
st[id] = max(st[id<<1], st[id<<1|1]);
}
void build(int pos, int x)
{
build(1, 1, n, pos, x);
}
int get(int id, int l, int r, int u, int v)
{
if(u <= l && r <= v)return st[id];
if(u > r || l > v)return 0;
int mid = (l+r)>>1;
return max(get(id<<1, l, mid, u, v), get(id<<1|1, mid+1, r, u, v));
}
int get(int l, int r)
{
return get(1, 1, n, l, r);
}
};
int mx, mn = inf;
bool check(int x)
{
bool ok = true;
for(int i = 1, p = m; i <= n; i ++)
{
for(int j = 1; j <= p; j ++)
{
if(mx-a[i][j] > x)
{
p = j-1;
break;
}
}
for(int j = p+1; j <= m; j ++)
{
if(a[i][j] - mn > x)
{
ok = false;
break;
}
}
}
if(ok)return true;
ok = true;
for(int i = 1, p = 1; i <= n; i ++)
{
for(int j = m; j >= p; j --)
{
if(mx-a[i][j] > x)
{
p = j+1;
break;
}
}
for(int j = p-1; j > 0; j --)
{
if(a[i][j] - mn > x)
{
ok = false;
break;
}
}
}
if(ok)return true;
ok = true;
for(int i = n, p = m; i > 0; i --)
{
for(int j = 1; j <= p; j ++)
{
if(mx-a[i][j] > x)
{
p = j-1;
break;
}
}
for(int j = p+1; j <= m; j ++)
{
if(a[i][j] - mn > x)
{
ok = false;
break;
}
}
}
if(ok)return true;
ok = true;
for(int i = n, p = 1; i > 0; i --)
{
for(int j = m; j >= p; j --)
{
if(mx-a[i][j] > x)
{
p = j+1;
break;
}
}
for(int j = p-1; j > 0; j --)
{
if(a[i][j] - mn > x)
{
ok = false;
break;
}
}
}
return ok;
}
void sol()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
cin >> a[i][j];
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
int l = 0, r = 1e9, mid;
while(l <= r)
{
mid = (l+r)>>1;
if(check(mid))r = mid-1;
else l = mid+1;
}
cout << l;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
//cin >> test;
while(test -- > 0)sol();
return 0;
}
Compilation message (stderr)
joioi.cpp: In function 'int main()':
joioi.cpp:191:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
191 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:192:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
192 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |