#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN = 2010;
int grid[2010][2010];
int n,m;
int mx;
int ans;
int mn = 1e9;
void revLin(int n, int m) {
for (int i = 1; i <= n / 2; ++i) {
for (int j = 1; j <= m; ++j) {
swap(grid[i][j], grid[n - i + 1][j]); } } }
void revCol(int n, int m) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m / 2; ++j) {
swap(grid[i][j], grid[i][m - j + 1]); } } }
bool find(int mid){
int temp = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(grid[i][j]<mx-mid){
temp = max(temp,j);
}
}
for(int j=1;j<=temp;j++){
if(grid[i][j]>mn+mid){
return false;
}
}
}
return true;
}
int check(){
int l = 0;
int r = mx-mn;
int ans = r;
while(l<=r){
int mid = (l+r)/2;
// cout<<mid<<endl;
if(find(mid)){
ans = mid;
r = mid-1;
}else{
l = mid+1;
}
}
return ans;
}
int main() {
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>grid[i][j];
mx = max(mx,grid[i][j]);
mn = min(mn,grid[i][j]);
}
}
ans = mx-mn;
ans = min(check(),ans);
revLin(n,m);
ans = min(check(),ans);
revCol(n,m);
ans = min(check(),ans);
revLin(n,m);
ans = min(check(),ans);
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
8 ms |
1400 KB |
Output is correct |
17 |
Correct |
16 ms |
1532 KB |
Output is correct |
18 |
Correct |
16 ms |
1524 KB |
Output is correct |
19 |
Correct |
17 ms |
1528 KB |
Output is correct |
20 |
Correct |
15 ms |
1448 KB |
Output is correct |
21 |
Correct |
20 ms |
1656 KB |
Output is correct |
22 |
Correct |
22 ms |
1660 KB |
Output is correct |
23 |
Correct |
21 ms |
1656 KB |
Output is correct |
24 |
Correct |
19 ms |
1468 KB |
Output is correct |
25 |
Correct |
23 ms |
1684 KB |
Output is correct |
26 |
Correct |
22 ms |
1656 KB |
Output is correct |
27 |
Correct |
20 ms |
1620 KB |
Output is correct |
28 |
Correct |
20 ms |
1784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
8 ms |
1400 KB |
Output is correct |
17 |
Correct |
16 ms |
1532 KB |
Output is correct |
18 |
Correct |
16 ms |
1524 KB |
Output is correct |
19 |
Correct |
17 ms |
1528 KB |
Output is correct |
20 |
Correct |
15 ms |
1448 KB |
Output is correct |
21 |
Correct |
20 ms |
1656 KB |
Output is correct |
22 |
Correct |
22 ms |
1660 KB |
Output is correct |
23 |
Correct |
21 ms |
1656 KB |
Output is correct |
24 |
Correct |
19 ms |
1468 KB |
Output is correct |
25 |
Correct |
23 ms |
1684 KB |
Output is correct |
26 |
Correct |
22 ms |
1656 KB |
Output is correct |
27 |
Correct |
20 ms |
1620 KB |
Output is correct |
28 |
Correct |
20 ms |
1784 KB |
Output is correct |
29 |
Correct |
1129 ms |
15352 KB |
Output is correct |
30 |
Correct |
1142 ms |
16248 KB |
Output is correct |
31 |
Correct |
1273 ms |
16172 KB |
Output is correct |
32 |
Correct |
1194 ms |
16380 KB |
Output is correct |
33 |
Correct |
1030 ms |
14328 KB |
Output is correct |
34 |
Correct |
1327 ms |
16344 KB |
Output is correct |
35 |
Correct |
1794 ms |
16216 KB |
Output is correct |
36 |
Correct |
1626 ms |
16152 KB |
Output is correct |
37 |
Correct |
1964 ms |
16340 KB |
Output is correct |