#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define xx first
#define yy second
#define srt(a) sort(a.begin(),a.end());
#define srtg(a,ll) sort(a.begin(),a.end(),greater<ll>())
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define up(a,x) upper_bound(a.begin(),a.end(),x)
#define fnd(a,x) find(a.begin(),a.end(),x)
#define vstart auto startt=chrono::system_clock::now()
#define vend auto endd=chrono::system_clock::now()
#define vvreme chrono::duration<double> vremee=endd-startt
#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<ll,ll> pll;
typedef string str;
const ll maxn=2010;
ll matra[maxn][maxn];
ll h,w;
ll maxi=LLONG_MIN;
ll mini=LLONG_MAX;
bool moze(ll raz){
ll dokle=LLONG_MAX;
//cout<<raz<<"\n";
for(ll i=0;i<h;i++){
for(ll j=0;j<w;j++){
if(matra[i][j]<maxi-raz){
dokle=min(dokle,j);
}
}
for(ll j=0;j<w;j++){
if(matra[i][j]>mini+raz && j>=dokle) return false;
}
}
return true;
}
void kolona(){
for(ll i=0;i<h/2;i++)
for(ll j=0;j<w;j++) swap(matra[i][j],matra[h-i-1][j]);
}
void red(){
for(ll i=0;i<h;i++)
for(ll j=0;j<w/2;j++) swap(matra[i][j],matra[i][w-j-1]);
}
void solve(){
cin>>h>>w;
for(ll i=0;i<h;i++)
for(ll j=0;j<w;j++){
cin>>matra[i][j];
maxi=max(maxi,matra[i][j]);
mini=min(mini,matra[i][j]);
}
ll res=LLONG_MAX;
auto nesto=[&](){
ll l=0;
ll r=1e9;
ll naj=-1;
while(l<=r){
ll mid=l+(r-l)/2;
if(moze(mid)){
r=mid-1;
naj=mid;
}else l=mid+1;
}
res=min(res,naj);
};
nesto();
kolona();
nesto();
red();
nesto();
kolona();
nesto();
cout<<res;
}
int main(){
ios;
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
316 KB |
Output is correct |
16 |
Correct |
15 ms |
1492 KB |
Output is correct |
17 |
Correct |
23 ms |
1612 KB |
Output is correct |
18 |
Correct |
15 ms |
1612 KB |
Output is correct |
19 |
Correct |
17 ms |
1708 KB |
Output is correct |
20 |
Correct |
22 ms |
1560 KB |
Output is correct |
21 |
Correct |
12 ms |
1736 KB |
Output is correct |
22 |
Correct |
15 ms |
1740 KB |
Output is correct |
23 |
Correct |
20 ms |
1828 KB |
Output is correct |
24 |
Correct |
12 ms |
1652 KB |
Output is correct |
25 |
Correct |
18 ms |
1824 KB |
Output is correct |
26 |
Correct |
13 ms |
1740 KB |
Output is correct |
27 |
Correct |
12 ms |
1740 KB |
Output is correct |
28 |
Correct |
12 ms |
1704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
316 KB |
Output is correct |
16 |
Correct |
15 ms |
1492 KB |
Output is correct |
17 |
Correct |
23 ms |
1612 KB |
Output is correct |
18 |
Correct |
15 ms |
1612 KB |
Output is correct |
19 |
Correct |
17 ms |
1708 KB |
Output is correct |
20 |
Correct |
22 ms |
1560 KB |
Output is correct |
21 |
Correct |
12 ms |
1736 KB |
Output is correct |
22 |
Correct |
15 ms |
1740 KB |
Output is correct |
23 |
Correct |
20 ms |
1828 KB |
Output is correct |
24 |
Correct |
12 ms |
1652 KB |
Output is correct |
25 |
Correct |
18 ms |
1824 KB |
Output is correct |
26 |
Correct |
13 ms |
1740 KB |
Output is correct |
27 |
Correct |
12 ms |
1740 KB |
Output is correct |
28 |
Correct |
12 ms |
1704 KB |
Output is correct |
29 |
Correct |
1189 ms |
30536 KB |
Output is correct |
30 |
Correct |
1276 ms |
32120 KB |
Output is correct |
31 |
Correct |
1451 ms |
32196 KB |
Output is correct |
32 |
Correct |
1304 ms |
32104 KB |
Output is correct |
33 |
Correct |
1105 ms |
28404 KB |
Output is correct |
34 |
Correct |
1398 ms |
32104 KB |
Output is correct |
35 |
Correct |
1055 ms |
32964 KB |
Output is correct |
36 |
Correct |
918 ms |
32324 KB |
Output is correct |
37 |
Correct |
1192 ms |
33224 KB |
Output is correct |