#include <bits/stdc++.h>
#include <bits/extc++.h>
//#define double long double
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(nullptr);
#define pb push_back
#define UNIQUE(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin());
#define ALL(X) X.begin(),X.end()
#define F(i,n) FF(i,0,(n))
#define F1(i,n) FF(i,1,(n)+1)
#define FF(i,n,m) for(int i=(int)n;i<int(m);++i)
#define rF(i,a,b) for(int i=a;i>=(int)b;--i)
#define mkp(a,b) make_pair(a,b)
#define Fi first
#define Se second
#define int ll
//#pragma GCC optimize("O3")//,unroll-loops
//#pragma GCC target("popcnt")
using namespace std;
template<typename T> bool remax(T& a, const T& b) {return b>a?a=b,1:0;}
template<typename T> bool remin(T& a, const T& b) {return b<a?a=b,1:0;}
template<typename T> void clear(T& a){
T b;
swap(a,b);
return;
}
using ll=long long;
using ull=unsigned long long;
using int128= __int128_t;
using uint128= __uint128_t;
using pii =pair<int,int> ;
const double pi=acos(-1);
const int N=2000+5;
const ll M=105;
const ll INF_64=0x3f3f3f3f3f3f3f3f;
const int INF_32=0x3f3f3f3f;
const int16_t INF_16=0x3f3f;
const int klog=20;
const int mod=1000000007;//1000000007;//998244353
const double eps=1E-3;
void gen(){
}
int a[N][N];
int mx=0,mn=INF_32;
int n,m;
bool check(int d){
int r=0;
F(i,n){
F(j,m)
if(a[i][j]<mx-d) remax(r,j+1);
F(j,m)
if(mn+d<a[i][j]&&j<r) return false;
}
return true;
}
int bin(){
int l=0,r=mx-mn;
while(l!=r){
int m=(l+r)/2;
if(check(m)) r=m;
else l=m+1;
}
return l;
}
void Rflip(){
F(i,n/2)
F(j,m)
swap(a[i][j],a[n-1-i][j]);
}
void Cflip(){
F(i,n)
F(j,m/2)
swap(a[i][j],a[i][m-1-j]);
}
void sol(){
cin>>n>>m;
F(i,n) F(j,m){
cin>>a[i][j];
if(!(i+j))mx=mn=a[0][0];
remax(mx,a[i][j]),remin(mn,a[i][j]);
}
int an=mx-mn;
remin(an,bin());
Rflip();
remin(an,bin());
Cflip();
remin(an,bin());
Rflip();
remin(an,bin());
cout<<an<<"\n";
}
int32_t main(){
#ifdef LOCAL
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
#endif // LOCAL
IOS;
int t=1;
gen();
//cin>>t;
FF(i,1,t+1){
//cout<<"Case #"<<i<<": ";
sol();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
1412 KB |
Output is correct |
17 |
Correct |
6 ms |
1616 KB |
Output is correct |
18 |
Correct |
7 ms |
1616 KB |
Output is correct |
19 |
Correct |
10 ms |
1668 KB |
Output is correct |
20 |
Correct |
7 ms |
1488 KB |
Output is correct |
21 |
Correct |
9 ms |
1744 KB |
Output is correct |
22 |
Correct |
11 ms |
1752 KB |
Output is correct |
23 |
Correct |
12 ms |
1744 KB |
Output is correct |
24 |
Correct |
9 ms |
1652 KB |
Output is correct |
25 |
Correct |
19 ms |
1824 KB |
Output is correct |
26 |
Correct |
11 ms |
1740 KB |
Output is correct |
27 |
Correct |
13 ms |
1744 KB |
Output is correct |
28 |
Correct |
9 ms |
1744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
1412 KB |
Output is correct |
17 |
Correct |
6 ms |
1616 KB |
Output is correct |
18 |
Correct |
7 ms |
1616 KB |
Output is correct |
19 |
Correct |
10 ms |
1668 KB |
Output is correct |
20 |
Correct |
7 ms |
1488 KB |
Output is correct |
21 |
Correct |
9 ms |
1744 KB |
Output is correct |
22 |
Correct |
11 ms |
1752 KB |
Output is correct |
23 |
Correct |
12 ms |
1744 KB |
Output is correct |
24 |
Correct |
9 ms |
1652 KB |
Output is correct |
25 |
Correct |
19 ms |
1824 KB |
Output is correct |
26 |
Correct |
11 ms |
1740 KB |
Output is correct |
27 |
Correct |
13 ms |
1744 KB |
Output is correct |
28 |
Correct |
9 ms |
1744 KB |
Output is correct |
29 |
Correct |
438 ms |
52164 KB |
Output is correct |
30 |
Correct |
473 ms |
53320 KB |
Output is correct |
31 |
Correct |
577 ms |
54848 KB |
Output is correct |
32 |
Correct |
403 ms |
54788 KB |
Output is correct |
33 |
Correct |
353 ms |
48004 KB |
Output is correct |
34 |
Correct |
505 ms |
54788 KB |
Output is correct |
35 |
Correct |
868 ms |
70340 KB |
Output is correct |
36 |
Correct |
747 ms |
65144 KB |
Output is correct |
37 |
Correct |
1057 ms |
70564 KB |
Output is correct |