#include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const ll inf = 1e18;
const int maxn = 2020;
int n,m;
ll g[maxn][maxn];
ll tmp[maxn][maxn];
ll dfs(int r, int hi, ll lo1, ll hi1, ll lo2, ll hi2) {
if (r==n) {
return max(hi1-lo1, hi2-lo2);
}
ll res = inf;
for (int i=hi; i>=0; i--) {
ll a = lo1;
ll b = hi1;
ll c = lo2;
ll d = hi2;
for (int j=0; j<m; j++) {
if (j<i) {
a = min(a,g[r][j]);
b = max(b,g[r][j]);
} else {
c = min(c,g[r][j]);
d = max(d,g[r][j]);
}
}
res = min(res, dfs(r+1,i,a,b,c,d));
}
return res;
}
ll solve() {
return dfs(0,m,inf,-inf,inf,-inf);
}
void flipY() {
for (int i=0; i<n; i++) {
for (int j=0; j<m/2; j++) {
swap(g[i][j],g[i][m-j-1]);
}
}
}
void transpose() {
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
tmp[j][i]=g[i][j];
}
}
swap(n,m);
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
g[i][j]=tmp[i][j];
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>m;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
cin>>g[i][j];
}
}
ll res = inf;
res = min(res, solve());
flipY();
res = min(res, solve());
flipY(); transpose();
res = min(res, solve());
flipY();
res = min(res, solve());
cout<<res<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
27 ms |
364 KB |
Output is correct |
4 |
Correct |
13 ms |
364 KB |
Output is correct |
5 |
Correct |
29 ms |
364 KB |
Output is correct |
6 |
Correct |
6 ms |
364 KB |
Output is correct |
7 |
Correct |
27 ms |
364 KB |
Output is correct |
8 |
Correct |
27 ms |
364 KB |
Output is correct |
9 |
Correct |
27 ms |
364 KB |
Output is correct |
10 |
Correct |
27 ms |
364 KB |
Output is correct |
11 |
Correct |
27 ms |
364 KB |
Output is correct |
12 |
Correct |
27 ms |
364 KB |
Output is correct |
13 |
Correct |
27 ms |
492 KB |
Output is correct |
14 |
Correct |
27 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
27 ms |
364 KB |
Output is correct |
4 |
Correct |
13 ms |
364 KB |
Output is correct |
5 |
Correct |
29 ms |
364 KB |
Output is correct |
6 |
Correct |
6 ms |
364 KB |
Output is correct |
7 |
Correct |
27 ms |
364 KB |
Output is correct |
8 |
Correct |
27 ms |
364 KB |
Output is correct |
9 |
Correct |
27 ms |
364 KB |
Output is correct |
10 |
Correct |
27 ms |
364 KB |
Output is correct |
11 |
Correct |
27 ms |
364 KB |
Output is correct |
12 |
Correct |
27 ms |
364 KB |
Output is correct |
13 |
Correct |
27 ms |
492 KB |
Output is correct |
14 |
Correct |
27 ms |
364 KB |
Output is correct |
15 |
Execution timed out |
4059 ms |
364 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
27 ms |
364 KB |
Output is correct |
4 |
Correct |
13 ms |
364 KB |
Output is correct |
5 |
Correct |
29 ms |
364 KB |
Output is correct |
6 |
Correct |
6 ms |
364 KB |
Output is correct |
7 |
Correct |
27 ms |
364 KB |
Output is correct |
8 |
Correct |
27 ms |
364 KB |
Output is correct |
9 |
Correct |
27 ms |
364 KB |
Output is correct |
10 |
Correct |
27 ms |
364 KB |
Output is correct |
11 |
Correct |
27 ms |
364 KB |
Output is correct |
12 |
Correct |
27 ms |
364 KB |
Output is correct |
13 |
Correct |
27 ms |
492 KB |
Output is correct |
14 |
Correct |
27 ms |
364 KB |
Output is correct |
15 |
Execution timed out |
4059 ms |
364 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |