#include "bits/stdc++.h"
#pragma GCC optimize "trapv"
using i64 = long long int;
using namespace std;
int nxt(){int x; cin >> x; return x;}
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define inarr(a,n) for(int i = 0; i < n; ++i) cin >> a[i];
#define int long long
int n,m,a[2005][2005],mx = 0, mn = INT_MAX;
void fliprow() {
for(int i = 1; i <= n / 2; i++) {
for(int j = 1; j <= m; j++) {
swap(a[i][j], a[n - i + 1][j]);
}
}
}
void flipcol() {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m / 2; j++) {
swap(a[i][j], a[i][m - j + 1]);
}
}
}
bool check(int mid){
int mxrow = n;
for(int j = 1; j <= m; ++j) {
for(int i = 1; i <= mxrow; ++i) {
if(mx - a[i][j] > mid) {
mxrow = i - 1;
break;
}
}
for(int i = mxrow + 1; i<= n; ++i) {
if(a[i][j] - mn > mid) {
return 0;
}
}
}
return 1;
}
int find(){
int l = 0, r = mx - mn, ans = r;
while(l <= r){
int mid = (l + r)/2;
if(check(mid)){
r = mid - 1;
ans = min(ans, mid);
} else l = mid + 1;
}
return ans;
}
void solve(){
cin >> n >> m;
for(int i=1;i<n+1;++i) for(int j=1;j<m+1;++j) {
cin >> a[i][j];
mn = min(mn, a[i][j]);
mx = max(mx ,a[i][j]);
}
int ans = find();
flipcol();
ans = min(ans, find());
fliprow();
ans = min(ans, find());
flipcol();
ans = min(ans, find());
cout << ans;
}
signed main(){
cin.tie(nullptr);cout.tie(nullptr);ios::sync_with_stdio(false);
int testt = 1;
// cin >> testt;
for (int i = 0; i < testt; ++i) {
// cout << "Case #" << i << ": ";
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 |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
332 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 |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
332 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 |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
5 ms |
1484 KB |
Output is correct |
17 |
Correct |
11 ms |
1616 KB |
Output is correct |
18 |
Correct |
11 ms |
1616 KB |
Output is correct |
19 |
Correct |
12 ms |
1716 KB |
Output is correct |
20 |
Correct |
11 ms |
1500 KB |
Output is correct |
21 |
Correct |
16 ms |
1824 KB |
Output is correct |
22 |
Correct |
16 ms |
1732 KB |
Output is correct |
23 |
Correct |
19 ms |
1740 KB |
Output is correct |
24 |
Correct |
16 ms |
1612 KB |
Output is correct |
25 |
Correct |
20 ms |
1824 KB |
Output is correct |
26 |
Correct |
19 ms |
1828 KB |
Output is correct |
27 |
Correct |
16 ms |
1716 KB |
Output is correct |
28 |
Correct |
18 ms |
1824 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 |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
332 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 |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
5 ms |
1484 KB |
Output is correct |
17 |
Correct |
11 ms |
1616 KB |
Output is correct |
18 |
Correct |
11 ms |
1616 KB |
Output is correct |
19 |
Correct |
12 ms |
1716 KB |
Output is correct |
20 |
Correct |
11 ms |
1500 KB |
Output is correct |
21 |
Correct |
16 ms |
1824 KB |
Output is correct |
22 |
Correct |
16 ms |
1732 KB |
Output is correct |
23 |
Correct |
19 ms |
1740 KB |
Output is correct |
24 |
Correct |
16 ms |
1612 KB |
Output is correct |
25 |
Correct |
20 ms |
1824 KB |
Output is correct |
26 |
Correct |
19 ms |
1828 KB |
Output is correct |
27 |
Correct |
16 ms |
1716 KB |
Output is correct |
28 |
Correct |
18 ms |
1824 KB |
Output is correct |
29 |
Correct |
966 ms |
52260 KB |
Output is correct |
30 |
Correct |
991 ms |
53228 KB |
Output is correct |
31 |
Correct |
1110 ms |
54820 KB |
Output is correct |
32 |
Correct |
867 ms |
54796 KB |
Output is correct |
33 |
Correct |
865 ms |
47908 KB |
Output is correct |
34 |
Correct |
1002 ms |
55036 KB |
Output is correct |
35 |
Correct |
2715 ms |
70404 KB |
Output is correct |
36 |
Correct |
2443 ms |
65228 KB |
Output is correct |
37 |
Correct |
2836 ms |
70492 KB |
Output is correct |