#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;int m;
int TPV;int BTV;
int gr[2010][2010];
int tgr[2010][2010];
int rmq[2010][12][2010];
int rmxq[2010][12][2010];
int ans;
int chmx(int id,int l,int r){
int lg=__lg(r-l+1);
return max(rmxq[id][lg][l],rmxq[id][lg][r-(1<<lg)+1]);
}
int chmn(int id,int l,int r){
int lg=__lg(r-l+1);
return min(rmq[id][lg][l],rmq[id][lg][r-(1<<lg)+1]);
}
void spin(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
tgr[j][n-i+1 ]=gr[i][j];
}
}
swap(n,m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
gr[i][j]=tgr[i][j];
}
}
}
void precalc(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
rmq[i][0][j]=gr[i][j];
rmxq[i][0][j]=gr[i][j];
}
}
for(int t=1;t<=11;t++){
for(int i=1;i<=n;i++){
for(int j=1;j+(1<<t)-1<=m;j++){
rmq[i][t][j]=min(rmq[i][t-1][j],rmq[i][t-1][j+(1<<(t-1))]);
rmxq[i][t][j]=max(rmxq[i][t-1][j],rmxq[i][t-1][j+(1<<(t-1))]);
}
}
}
}
int ok1(int mid){
int lst=m;
int tp=TPV-mid;
int bt=BTV+mid;
for(int i=1;i<=n;i++){
int l=0;int r=lst;
while(l<r){
int mi=l+(r-l+1)/2;
if((mi==0||(mi!=0&&chmn(i,1,mi)>=tp)) ){
l=mi;
}else{
r=mi-1;
}
}
lst=l;
if((lst!=m&&chmx(i,lst+1,m)>bt) ){return 0;}
}
return 1;
}
int ok2(int mid){
int lst=m;
int tp=TPV-mid;
int bt=BTV+mid;
for(int i=1;i<=n;i++){
int l=0;int r=lst;
while(l<r){
int mi=l+(r-l+1)/2;
if((mi==0||(mi!=0&&chmx(i,1,mi)<=bt)) ){
l=mi;
}else{
r=mi-1;
}
}
lst=l;
if((lst!=m&&chmn(i,lst+1,m)<tp) ){return 0;}
}
return 1;
}
int solve(){
int l=0;int r=1e9;
while(l<r){
int mi=l+(r-l)/2;
if((ok1(mi)||ok2(mi))){
r=mi;
}else{
l=mi+1;
}
}
return l;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>gr[i][j];
}
}
TPV=gr[1][1];BTV=gr[1][1];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
TPV=max(TPV,gr[i][j]);
BTV=min(BTV,gr[i][j]);
}
}
ans=1e9;
precalc();
ans=solve();
for(int t=1;t<=3;t++){
spin();
precalc();
ans=min(ans,solve());
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
696 KB |
Output is correct |
13 |
Correct |
1 ms |
692 KB |
Output is correct |
14 |
Correct |
1 ms |
692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
696 KB |
Output is correct |
13 |
Correct |
1 ms |
692 KB |
Output is correct |
14 |
Correct |
1 ms |
692 KB |
Output is correct |
15 |
Correct |
4 ms |
9280 KB |
Output is correct |
16 |
Correct |
17 ms |
19652 KB |
Output is correct |
17 |
Correct |
23 ms |
19924 KB |
Output is correct |
18 |
Correct |
26 ms |
19876 KB |
Output is correct |
19 |
Correct |
25 ms |
19948 KB |
Output is correct |
20 |
Correct |
22 ms |
19808 KB |
Output is correct |
21 |
Correct |
29 ms |
20004 KB |
Output is correct |
22 |
Correct |
26 ms |
20048 KB |
Output is correct |
23 |
Correct |
26 ms |
19968 KB |
Output is correct |
24 |
Correct |
24 ms |
19932 KB |
Output is correct |
25 |
Correct |
26 ms |
20044 KB |
Output is correct |
26 |
Correct |
27 ms |
19960 KB |
Output is correct |
27 |
Correct |
26 ms |
20000 KB |
Output is correct |
28 |
Correct |
27 ms |
19992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
696 KB |
Output is correct |
13 |
Correct |
1 ms |
692 KB |
Output is correct |
14 |
Correct |
1 ms |
692 KB |
Output is correct |
15 |
Correct |
4 ms |
9280 KB |
Output is correct |
16 |
Correct |
17 ms |
19652 KB |
Output is correct |
17 |
Correct |
23 ms |
19924 KB |
Output is correct |
18 |
Correct |
26 ms |
19876 KB |
Output is correct |
19 |
Correct |
25 ms |
19948 KB |
Output is correct |
20 |
Correct |
22 ms |
19808 KB |
Output is correct |
21 |
Correct |
29 ms |
20004 KB |
Output is correct |
22 |
Correct |
26 ms |
20048 KB |
Output is correct |
23 |
Correct |
26 ms |
19968 KB |
Output is correct |
24 |
Correct |
24 ms |
19932 KB |
Output is correct |
25 |
Correct |
26 ms |
20044 KB |
Output is correct |
26 |
Correct |
27 ms |
19960 KB |
Output is correct |
27 |
Correct |
26 ms |
20000 KB |
Output is correct |
28 |
Correct |
27 ms |
19992 KB |
Output is correct |
29 |
Runtime error |
1063 ms |
262144 KB |
Execution killed with signal 9 |
30 |
Halted |
0 ms |
0 KB |
- |