#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long lld;
const int MX=2010, inf=2e9;
inline int _max(int x, int y){ return x>y ? x : y; }
inline int _min(int x, int y){ return x<y ? x : y; }
int B[MX][MX];
int w, h;
struct node {
int val, x, y;
bool used;
};
int D[MX][MX];
node V[MX*MX];
int solve(){
int hi=0;
for(int i=1, idx=0; i<=h; i++)
for(int j=1; j<=w; j++){
V[idx++]={B[i][j], i, j, false};
hi=_max(hi, B[i][j]);
}
sort(V, V+h*w, [](node &a, node &b){
if(a.val==b.val){
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
return a.val<b.val;
});
for(int i=0; i<h*w; i++){
node &nd=V[i];
D[nd.x][nd.y]=i;
}
int pos[MX]={}; pos[0]=w;
int mn=inf, mx=0, ans=inf, cnt=0;
for(int idx=0, nah=0; idx<h*w; idx++){
int val=V[idx].val, i=V[idx].x;
for(; i<=h; i++){
bool uptd=false;
while(pos[i]<pos[i-1] && B[i][pos[i]+1]<=val){
uptd=true; pos[i]++;
int now=B[i][pos[i]];
mn=_min(mn, now), mx=_max(mx, now);
V[D[i][pos[i]]].used=true;
cnt++;
}
if(!uptd) break;
}
if(cnt==0 || cnt==w*h) continue;
while(nah<=w*h-1 && V[nah].used) nah++;
ans=_min(ans, _max(mx-mn, hi-V[nah].val) );
}
return ans;
}
void flip_v(){
for(int i=1; i<=h/2; i++)
for(int j=1; j<=w; j++)
swap(B[i][j], B[h-i+1][j]);
}
void flip_h(){
for(int i=1; i<=h; i++)
for(int j=1; j<=w/2; j++)
swap(B[i][j], B[i][w-j+1]);
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>h>>w;
for(int i=1; i<=h; i++)
for(int j=1; j<=w; j++)
cin>>B[i][j];
int ans=solve();
flip_v();
ans=min(ans, solve());
flip_h();
ans=min(ans, solve());
flip_v();
ans=min(ans, solve());
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
672 KB |
Output is correct |
4 |
Correct |
2 ms |
672 KB |
Output is correct |
5 |
Correct |
2 ms |
796 KB |
Output is correct |
6 |
Correct |
2 ms |
796 KB |
Output is correct |
7 |
Correct |
2 ms |
796 KB |
Output is correct |
8 |
Correct |
2 ms |
796 KB |
Output is correct |
9 |
Correct |
2 ms |
796 KB |
Output is correct |
10 |
Correct |
2 ms |
796 KB |
Output is correct |
11 |
Correct |
2 ms |
796 KB |
Output is correct |
12 |
Correct |
2 ms |
796 KB |
Output is correct |
13 |
Correct |
2 ms |
796 KB |
Output is correct |
14 |
Correct |
3 ms |
796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
672 KB |
Output is correct |
4 |
Correct |
2 ms |
672 KB |
Output is correct |
5 |
Correct |
2 ms |
796 KB |
Output is correct |
6 |
Correct |
2 ms |
796 KB |
Output is correct |
7 |
Correct |
2 ms |
796 KB |
Output is correct |
8 |
Correct |
2 ms |
796 KB |
Output is correct |
9 |
Correct |
2 ms |
796 KB |
Output is correct |
10 |
Correct |
2 ms |
796 KB |
Output is correct |
11 |
Correct |
2 ms |
796 KB |
Output is correct |
12 |
Correct |
2 ms |
796 KB |
Output is correct |
13 |
Correct |
2 ms |
796 KB |
Output is correct |
14 |
Correct |
3 ms |
796 KB |
Output is correct |
15 |
Correct |
3 ms |
796 KB |
Output is correct |
16 |
Correct |
27 ms |
3068 KB |
Output is correct |
17 |
Correct |
24 ms |
3068 KB |
Output is correct |
18 |
Correct |
24 ms |
3068 KB |
Output is correct |
19 |
Correct |
24 ms |
3192 KB |
Output is correct |
20 |
Correct |
22 ms |
3192 KB |
Output is correct |
21 |
Correct |
25 ms |
3192 KB |
Output is correct |
22 |
Correct |
27 ms |
3192 KB |
Output is correct |
23 |
Correct |
25 ms |
3192 KB |
Output is correct |
24 |
Correct |
22 ms |
3192 KB |
Output is correct |
25 |
Correct |
25 ms |
3192 KB |
Output is correct |
26 |
Correct |
28 ms |
3324 KB |
Output is correct |
27 |
Correct |
26 ms |
3324 KB |
Output is correct |
28 |
Correct |
29 ms |
3324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
672 KB |
Output is correct |
4 |
Correct |
2 ms |
672 KB |
Output is correct |
5 |
Correct |
2 ms |
796 KB |
Output is correct |
6 |
Correct |
2 ms |
796 KB |
Output is correct |
7 |
Correct |
2 ms |
796 KB |
Output is correct |
8 |
Correct |
2 ms |
796 KB |
Output is correct |
9 |
Correct |
2 ms |
796 KB |
Output is correct |
10 |
Correct |
2 ms |
796 KB |
Output is correct |
11 |
Correct |
2 ms |
796 KB |
Output is correct |
12 |
Correct |
2 ms |
796 KB |
Output is correct |
13 |
Correct |
2 ms |
796 KB |
Output is correct |
14 |
Correct |
3 ms |
796 KB |
Output is correct |
15 |
Correct |
3 ms |
796 KB |
Output is correct |
16 |
Correct |
27 ms |
3068 KB |
Output is correct |
17 |
Correct |
24 ms |
3068 KB |
Output is correct |
18 |
Correct |
24 ms |
3068 KB |
Output is correct |
19 |
Correct |
24 ms |
3192 KB |
Output is correct |
20 |
Correct |
22 ms |
3192 KB |
Output is correct |
21 |
Correct |
25 ms |
3192 KB |
Output is correct |
22 |
Correct |
27 ms |
3192 KB |
Output is correct |
23 |
Correct |
25 ms |
3192 KB |
Output is correct |
24 |
Correct |
22 ms |
3192 KB |
Output is correct |
25 |
Correct |
25 ms |
3192 KB |
Output is correct |
26 |
Correct |
28 ms |
3324 KB |
Output is correct |
27 |
Correct |
26 ms |
3324 KB |
Output is correct |
28 |
Correct |
29 ms |
3324 KB |
Output is correct |
29 |
Correct |
3531 ms |
90004 KB |
Output is correct |
30 |
Correct |
3414 ms |
90316 KB |
Output is correct |
31 |
Correct |
3769 ms |
94608 KB |
Output is correct |
32 |
Correct |
3670 ms |
94756 KB |
Output is correct |
33 |
Correct |
3209 ms |
94756 KB |
Output is correct |
34 |
Correct |
3819 ms |
94848 KB |
Output is correct |
35 |
Correct |
3614 ms |
94868 KB |
Output is correct |
36 |
Correct |
3136 ms |
94868 KB |
Output is correct |
37 |
Correct |
3573 ms |
94868 KB |
Output is correct |