#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e3 + 2, MOD = (int)1e9 + 7;
int n,m,a[N][N],b[N][N];
void rot(){
// for(int i = 1;i <= n;i++){
// for(int j = 1;j <= m;j++){
// cout << a[i][j] << ' ';
// }
// cout << '\n';
// }
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
a[m - j + 1][i] = b[i][j];
}
}
swap(n,m);
// cout << "rot\n";
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
b[i][j] = a[i][j];
// cout << a[i][j] << ' ';
}
// cout << '\n';
}
// cout << '\n';
}
int mn = 1e9;
int it[N];
bool check(int mid){
int l = mn,r = l + mid;
it[0] = m;
for(int i = 1;i <= n;i++){
it[i] = 0;
for(int j = 1;j <= it[i - 1] && a[i][j] >= l && a[i][j] <= r;j++){
it[i] = j;
}
}
set<int> st;
for(int i = 1;i <= n;i++){
for(int j = it[i] + 1;j <= m;j++){
st.insert(a[i][j]);
}
}
return (st.empty() || (*st.rbegin() - *st.begin() <= mid));
}
bool ok(int mid){
bool res = false;
for(int i = 0;i < 4;i++){
if(check(mid)){
res = 1;
}
rot();
}
return res;
}
void test(){
cin >> n >> m;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;++j){
cin >> a[i][j];
b[i][j] = a[i][j];
mn = min(mn,a[i][j]);
}
}
int l = -1,r = 1e9 + 1;
while(r - l > 1){
int mid = (l + r) >> 1;
if(ok(mid)){
r = mid;
}else{
l = mid;
}
// cout << mid << ' ' << ok(mid) << ' ' << check(mid) << '\n';
}
cout << r << '\n';
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--){
test();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2464 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2464 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
13 ms |
6792 KB |
Output is correct |
17 |
Correct |
241 ms |
8796 KB |
Output is correct |
18 |
Correct |
491 ms |
9040 KB |
Output is correct |
19 |
Correct |
499 ms |
8796 KB |
Output is correct |
20 |
Correct |
424 ms |
8536 KB |
Output is correct |
21 |
Correct |
471 ms |
8792 KB |
Output is correct |
22 |
Correct |
693 ms |
9044 KB |
Output is correct |
23 |
Correct |
654 ms |
8968 KB |
Output is correct |
24 |
Correct |
656 ms |
8536 KB |
Output is correct |
25 |
Correct |
718 ms |
8920 KB |
Output is correct |
26 |
Correct |
654 ms |
8796 KB |
Output is correct |
27 |
Correct |
524 ms |
9044 KB |
Output is correct |
28 |
Correct |
467 ms |
8792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2464 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
13 ms |
6792 KB |
Output is correct |
17 |
Correct |
241 ms |
8796 KB |
Output is correct |
18 |
Correct |
491 ms |
9040 KB |
Output is correct |
19 |
Correct |
499 ms |
8796 KB |
Output is correct |
20 |
Correct |
424 ms |
8536 KB |
Output is correct |
21 |
Correct |
471 ms |
8792 KB |
Output is correct |
22 |
Correct |
693 ms |
9044 KB |
Output is correct |
23 |
Correct |
654 ms |
8968 KB |
Output is correct |
24 |
Correct |
656 ms |
8536 KB |
Output is correct |
25 |
Correct |
718 ms |
8920 KB |
Output is correct |
26 |
Correct |
654 ms |
8796 KB |
Output is correct |
27 |
Correct |
524 ms |
9044 KB |
Output is correct |
28 |
Correct |
467 ms |
8792 KB |
Output is correct |
29 |
Execution timed out |
4041 ms |
58504 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |