#include<bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
const ld PI = 3.1415926535;
const int N = 5000+10;
const int M = 7e6 + 1;
int mod = 998244353;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 2;
int mult(int a, int b) {
return a * 1LL * b % mod;
}
int sum(int a, int b) {
if (a + b < 0)
return a + b + mod;
if (a + b >= mod)
return a + b - mod;
return a + b;
}
ll binpow(ll a, ll n) {
if (n == 0)
return 1;
if (n % 2 == 1) {
return binpow(a, n - 1) * a % mod;
} else {
ll b = binpow(a, n / 2);
return b * b % mod;
}
}
int H, W, mat[N][N], tmp[N][N];
void rotate(){
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
tmp[W - j - 1][i] = mat[i][j];
}
}
for(int i = 0; i < W; i++)
for(int j = 0; j < H; j++)
mat[i][j] = tmp[i][j];
swap(H, W);
}
bool check(int mxd){
int ndmn = infi;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
ndmn = min(ndmn, mat[i][j]);
}
}
int mx = mxd + ndmn;
int cmx = W - 1;
int dmn = infi, dmx = -infi;
for(int i = 0; i < H; i++){
int ha = 0;
for(; ha <= cmx; ha++){
if(mat[i][ha] > mx)
break;
}
cmx = ha - 1;
//cout << ha << '\n';
for(int j = ha; j < W; j++){
dmn = min(dmn, mat[i][j]);
dmx = max(dmx, mat[i][j]);
}
}
//cout << '\n';
return (dmx - dmn <= mxd);
}
void solve(){
cin >> H >> W;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++)
cin >> mat[i][j];
}
/*cout << '\n';
for(int k = 0; k < 4; k++){
for(int i = 0; i < sz(mat); i++)
for(int j = 0; j < sz(mat[i]); j++)
cout << mat[i][j] << " \n"[sz(mat[i]) - 1 == j];
rotate();
cout << '\n';
}*/
int lo = -1, hi = 1e9;
while(lo + 1 < hi){
int m = (lo + hi) / 2;
bool ok = false;
for(int i = 0; i < 4; i++){
if(check(m))
ok = true;
rotate();
}
if(ok)
hi = m;
else
lo = m;
}
cout << hi << '\n';
}
signed main() {
mispertion;
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4520 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4520 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
16988 KB |
Output is correct |
16 |
Correct |
14 ms |
15448 KB |
Output is correct |
17 |
Correct |
16 ms |
15448 KB |
Output is correct |
18 |
Correct |
16 ms |
18780 KB |
Output is correct |
19 |
Correct |
16 ms |
16988 KB |
Output is correct |
20 |
Correct |
15 ms |
15452 KB |
Output is correct |
21 |
Correct |
16 ms |
15192 KB |
Output is correct |
22 |
Correct |
17 ms |
18780 KB |
Output is correct |
23 |
Correct |
16 ms |
15460 KB |
Output is correct |
24 |
Correct |
15 ms |
18780 KB |
Output is correct |
25 |
Correct |
22 ms |
18920 KB |
Output is correct |
26 |
Correct |
18 ms |
18780 KB |
Output is correct |
27 |
Correct |
17 ms |
18780 KB |
Output is correct |
28 |
Correct |
16 ms |
18924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4520 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
16988 KB |
Output is correct |
16 |
Correct |
14 ms |
15448 KB |
Output is correct |
17 |
Correct |
16 ms |
15448 KB |
Output is correct |
18 |
Correct |
16 ms |
18780 KB |
Output is correct |
19 |
Correct |
16 ms |
16988 KB |
Output is correct |
20 |
Correct |
15 ms |
15452 KB |
Output is correct |
21 |
Correct |
16 ms |
15192 KB |
Output is correct |
22 |
Correct |
17 ms |
18780 KB |
Output is correct |
23 |
Correct |
16 ms |
15460 KB |
Output is correct |
24 |
Correct |
15 ms |
18780 KB |
Output is correct |
25 |
Correct |
22 ms |
18920 KB |
Output is correct |
26 |
Correct |
18 ms |
18780 KB |
Output is correct |
27 |
Correct |
17 ms |
18780 KB |
Output is correct |
28 |
Correct |
16 ms |
18924 KB |
Output is correct |
29 |
Correct |
2229 ms |
94064 KB |
Output is correct |
30 |
Correct |
2139 ms |
92836 KB |
Output is correct |
31 |
Correct |
2417 ms |
92112 KB |
Output is correct |
32 |
Correct |
2453 ms |
92144 KB |
Output is correct |
33 |
Correct |
2118 ms |
111440 KB |
Output is correct |
34 |
Correct |
2308 ms |
116304 KB |
Output is correct |
35 |
Correct |
2427 ms |
132812 KB |
Output is correct |
36 |
Correct |
2045 ms |
126036 KB |
Output is correct |
37 |
Correct |
2623 ms |
134076 KB |
Output is correct |