#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
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
480 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
484 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
476 KB |
Output is correct |
14 |
Correct |
1 ms |
480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
480 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
484 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
476 KB |
Output is correct |
14 |
Correct |
1 ms |
480 KB |
Output is correct |
15 |
Correct |
1 ms |
484 KB |
Output is correct |
16 |
Correct |
3 ms |
1388 KB |
Output is correct |
17 |
Correct |
5 ms |
1648 KB |
Output is correct |
18 |
Correct |
6 ms |
1612 KB |
Output is correct |
19 |
Correct |
6 ms |
1648 KB |
Output is correct |
20 |
Correct |
5 ms |
1476 KB |
Output is correct |
21 |
Correct |
11 ms |
1744 KB |
Output is correct |
22 |
Correct |
6 ms |
1760 KB |
Output is correct |
23 |
Correct |
9 ms |
1748 KB |
Output is correct |
24 |
Correct |
6 ms |
1640 KB |
Output is correct |
25 |
Correct |
6 ms |
1752 KB |
Output is correct |
26 |
Correct |
6 ms |
1748 KB |
Output is correct |
27 |
Correct |
7 ms |
1740 KB |
Output is correct |
28 |
Correct |
7 ms |
1708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
480 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
484 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
476 KB |
Output is correct |
14 |
Correct |
1 ms |
480 KB |
Output is correct |
15 |
Correct |
1 ms |
484 KB |
Output is correct |
16 |
Correct |
3 ms |
1388 KB |
Output is correct |
17 |
Correct |
5 ms |
1648 KB |
Output is correct |
18 |
Correct |
6 ms |
1612 KB |
Output is correct |
19 |
Correct |
6 ms |
1648 KB |
Output is correct |
20 |
Correct |
5 ms |
1476 KB |
Output is correct |
21 |
Correct |
11 ms |
1744 KB |
Output is correct |
22 |
Correct |
6 ms |
1760 KB |
Output is correct |
23 |
Correct |
9 ms |
1748 KB |
Output is correct |
24 |
Correct |
6 ms |
1640 KB |
Output is correct |
25 |
Correct |
6 ms |
1752 KB |
Output is correct |
26 |
Correct |
6 ms |
1748 KB |
Output is correct |
27 |
Correct |
7 ms |
1740 KB |
Output is correct |
28 |
Correct |
7 ms |
1708 KB |
Output is correct |
29 |
Correct |
352 ms |
15432 KB |
Output is correct |
30 |
Correct |
308 ms |
16156 KB |
Output is correct |
31 |
Correct |
370 ms |
16132 KB |
Output is correct |
32 |
Correct |
330 ms |
16212 KB |
Output is correct |
33 |
Correct |
363 ms |
14292 KB |
Output is correct |
34 |
Correct |
319 ms |
16268 KB |
Output is correct |
35 |
Correct |
475 ms |
16440 KB |
Output is correct |
36 |
Correct |
425 ms |
16180 KB |
Output is correct |
37 |
Correct |
433 ms |
16200 KB |
Output is correct |