#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];
bool test(ll diff) {
ll mn = inf;
ll mx = -inf;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
mn = min(mn, g[i][j]);
mx = max(mx, g[i][j]);
}
}
{
int sep = 0;
bool ok = true;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (g[i][j]<mx-diff) {
sep = max(sep,j+1);
}
}
for (int j=0; j<m; j++) {
if (g[i][j]>mn+diff) {
if (j<sep) ok = false;
}
}
}
if (ok) return true;
}
{
int sep = 0;
bool ok = true;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (g[i][j]>mn+diff) {
sep = max(sep,j+1);
}
}
for (int j=0; j<m; j++) {
if (g[i][j]<mx-diff) {
if (j<sep) ok = false;
}
}
}
if (ok) return true;
}
return false;
}
ll solve() {
ll mn = inf;
ll mx = -inf;
vector<ll> a;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
mn = min(mn, g[i][j]);
mx = max(mx, g[i][j]);
}
}
ll diff = mx-mn;
ll hi = diff;
ll lo = -1;
while (hi-lo>1) {
ll mid = (lo+hi)/2;
if (test(mid)) {
hi = mid;
} else {
lo = mid;
}
}
return hi;
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
2028 KB |
Output is correct |
16 |
Correct |
15 ms |
2540 KB |
Output is correct |
17 |
Correct |
33 ms |
2796 KB |
Output is correct |
18 |
Correct |
29 ms |
2820 KB |
Output is correct |
19 |
Correct |
34 ms |
2816 KB |
Output is correct |
20 |
Correct |
33 ms |
2796 KB |
Output is correct |
21 |
Correct |
44 ms |
2924 KB |
Output is correct |
22 |
Correct |
38 ms |
2924 KB |
Output is correct |
23 |
Correct |
47 ms |
2924 KB |
Output is correct |
24 |
Correct |
34 ms |
2944 KB |
Output is correct |
25 |
Correct |
57 ms |
2924 KB |
Output is correct |
26 |
Correct |
42 ms |
2956 KB |
Output is correct |
27 |
Correct |
43 ms |
2924 KB |
Output is correct |
28 |
Correct |
42 ms |
2924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
2028 KB |
Output is correct |
16 |
Correct |
15 ms |
2540 KB |
Output is correct |
17 |
Correct |
33 ms |
2796 KB |
Output is correct |
18 |
Correct |
29 ms |
2820 KB |
Output is correct |
19 |
Correct |
34 ms |
2816 KB |
Output is correct |
20 |
Correct |
33 ms |
2796 KB |
Output is correct |
21 |
Correct |
44 ms |
2924 KB |
Output is correct |
22 |
Correct |
38 ms |
2924 KB |
Output is correct |
23 |
Correct |
47 ms |
2924 KB |
Output is correct |
24 |
Correct |
34 ms |
2944 KB |
Output is correct |
25 |
Correct |
57 ms |
2924 KB |
Output is correct |
26 |
Correct |
42 ms |
2956 KB |
Output is correct |
27 |
Correct |
43 ms |
2924 KB |
Output is correct |
28 |
Correct |
42 ms |
2924 KB |
Output is correct |
29 |
Correct |
2809 ms |
85740 KB |
Output is correct |
30 |
Correct |
2692 ms |
83012 KB |
Output is correct |
31 |
Correct |
3441 ms |
86892 KB |
Output is correct |
32 |
Correct |
2615 ms |
86764 KB |
Output is correct |
33 |
Correct |
2193 ms |
83564 KB |
Output is correct |
34 |
Correct |
3052 ms |
86896 KB |
Output is correct |
35 |
Execution timed out |
4101 ms |
102420 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |