#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long lld;
const int MX=2010, inf=2e9;
int B[MX][MX];
int w, h;
int solve(){
multiset<int> J;
vector<pii> V;
for(int i=1; i<=h; i++)
for(int j=1; j<=w; j++)
J.insert(B[i][j]), V.push_back({B[i][j], i});
sort(V.begin(), V.end());
int pos[MX]={}; pos[0]=w;
int mn=inf, mx=0, ans=inf;
for(pii &p:V){
int val, i; tie(val, i)=p;
for(; i<=h; i++){
while(pos[i]<pos[i-1] && B[i][pos[i]+1]<=val){
pos[i]++;
int now=B[i][pos[i]];
mn=min(mn, now), mx=max(mx, now);
J.erase(J.find(now));
}
}
if((int)J.size()==w*h || J.empty()) continue;
ans=min(ans, max(mx-mn, *J.rbegin()-*J.begin()));
}
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 |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
3 ms |
560 KB |
Output is correct |
5 |
Correct |
2 ms |
560 KB |
Output is correct |
6 |
Correct |
2 ms |
560 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
720 KB |
Output is correct |
11 |
Correct |
2 ms |
720 KB |
Output is correct |
12 |
Correct |
2 ms |
744 KB |
Output is correct |
13 |
Correct |
3 ms |
744 KB |
Output is correct |
14 |
Correct |
2 ms |
744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
3 ms |
560 KB |
Output is correct |
5 |
Correct |
2 ms |
560 KB |
Output is correct |
6 |
Correct |
2 ms |
560 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
720 KB |
Output is correct |
11 |
Correct |
2 ms |
720 KB |
Output is correct |
12 |
Correct |
2 ms |
744 KB |
Output is correct |
13 |
Correct |
3 ms |
744 KB |
Output is correct |
14 |
Correct |
2 ms |
744 KB |
Output is correct |
15 |
Correct |
5 ms |
764 KB |
Output is correct |
16 |
Correct |
106 ms |
3652 KB |
Output is correct |
17 |
Correct |
173 ms |
3824 KB |
Output is correct |
18 |
Correct |
192 ms |
3824 KB |
Output is correct |
19 |
Correct |
160 ms |
3824 KB |
Output is correct |
20 |
Correct |
143 ms |
3824 KB |
Output is correct |
21 |
Correct |
166 ms |
3824 KB |
Output is correct |
22 |
Correct |
155 ms |
3824 KB |
Output is correct |
23 |
Correct |
161 ms |
3824 KB |
Output is correct |
24 |
Correct |
137 ms |
3824 KB |
Output is correct |
25 |
Correct |
163 ms |
3824 KB |
Output is correct |
26 |
Correct |
169 ms |
3824 KB |
Output is correct |
27 |
Correct |
165 ms |
3824 KB |
Output is correct |
28 |
Correct |
169 ms |
3824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
3 ms |
560 KB |
Output is correct |
5 |
Correct |
2 ms |
560 KB |
Output is correct |
6 |
Correct |
2 ms |
560 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
720 KB |
Output is correct |
11 |
Correct |
2 ms |
720 KB |
Output is correct |
12 |
Correct |
2 ms |
744 KB |
Output is correct |
13 |
Correct |
3 ms |
744 KB |
Output is correct |
14 |
Correct |
2 ms |
744 KB |
Output is correct |
15 |
Correct |
5 ms |
764 KB |
Output is correct |
16 |
Correct |
106 ms |
3652 KB |
Output is correct |
17 |
Correct |
173 ms |
3824 KB |
Output is correct |
18 |
Correct |
192 ms |
3824 KB |
Output is correct |
19 |
Correct |
160 ms |
3824 KB |
Output is correct |
20 |
Correct |
143 ms |
3824 KB |
Output is correct |
21 |
Correct |
166 ms |
3824 KB |
Output is correct |
22 |
Correct |
155 ms |
3824 KB |
Output is correct |
23 |
Correct |
161 ms |
3824 KB |
Output is correct |
24 |
Correct |
137 ms |
3824 KB |
Output is correct |
25 |
Correct |
163 ms |
3824 KB |
Output is correct |
26 |
Correct |
169 ms |
3824 KB |
Output is correct |
27 |
Correct |
165 ms |
3824 KB |
Output is correct |
28 |
Correct |
169 ms |
3824 KB |
Output is correct |
29 |
Execution timed out |
4025 ms |
147136 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |