#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 21;
const int maxn = 2e3 + 7;
const ll oo = 1e9 + 69;
int n, m, a[maxn][maxn], mx, mn, b[maxn][maxn];
bool ok(int d) {
int last = 0;
for(int i = 1; i <= n; i++) {
int r = m;
for(int j = m; j >= 1; j--) {
if(a[i][j] >= mx - d) r--;
else break;
}
maxi(last, r);
for(int j = 1; j <= last; j++) {
if(a[i][j] > mn + d) return 0;
}
}
return 1;
}
void flip() {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
b[j][n - i + 1] = a[i][j];
}
}
swap(n, m);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) a[i][j] = b[i][j];
}
}
bool check(int d) {
int ans = 0;
for(int i = 0; i < 4; i++) {
ans |= ok(d);
flip();
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n>>m;
mx = 0;
mn = oo;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin>>a[i][j];
maxi(mx, a[i][j]);
mini(mn, a[i][j]);
}
}
int l = 0, r = 1e9, mid, res;
while(l <= r) {
mid = l + r >> 1;
if(check(mid)) {
res = mid;
r = mid - 1;
} else l = mid + 1;
}
cout<<res;
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:83:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
83 | mid = l + r >> 1;
| ~~^~~
joioi.cpp:89:11: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
89 | cout<<res;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 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 |
2508 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 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 |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 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 |
2508 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 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 |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
6 ms |
6748 KB |
Output is correct |
17 |
Correct |
8 ms |
7000 KB |
Output is correct |
18 |
Correct |
8 ms |
6748 KB |
Output is correct |
19 |
Correct |
11 ms |
6744 KB |
Output is correct |
20 |
Correct |
6 ms |
6860 KB |
Output is correct |
21 |
Correct |
8 ms |
7004 KB |
Output is correct |
22 |
Correct |
7 ms |
7004 KB |
Output is correct |
23 |
Correct |
7 ms |
7004 KB |
Output is correct |
24 |
Correct |
7 ms |
6964 KB |
Output is correct |
25 |
Correct |
7 ms |
7000 KB |
Output is correct |
26 |
Correct |
7 ms |
7004 KB |
Output is correct |
27 |
Correct |
8 ms |
7004 KB |
Output is correct |
28 |
Correct |
8 ms |
7120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 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 |
2508 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 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 |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
6 ms |
6748 KB |
Output is correct |
17 |
Correct |
8 ms |
7000 KB |
Output is correct |
18 |
Correct |
8 ms |
6748 KB |
Output is correct |
19 |
Correct |
11 ms |
6744 KB |
Output is correct |
20 |
Correct |
6 ms |
6860 KB |
Output is correct |
21 |
Correct |
8 ms |
7004 KB |
Output is correct |
22 |
Correct |
7 ms |
7004 KB |
Output is correct |
23 |
Correct |
7 ms |
7004 KB |
Output is correct |
24 |
Correct |
7 ms |
6964 KB |
Output is correct |
25 |
Correct |
7 ms |
7000 KB |
Output is correct |
26 |
Correct |
7 ms |
7004 KB |
Output is correct |
27 |
Correct |
8 ms |
7004 KB |
Output is correct |
28 |
Correct |
8 ms |
7120 KB |
Output is correct |
29 |
Correct |
1832 ms |
54008 KB |
Output is correct |
30 |
Correct |
1770 ms |
53680 KB |
Output is correct |
31 |
Correct |
1986 ms |
55064 KB |
Output is correct |
32 |
Correct |
1890 ms |
55020 KB |
Output is correct |
33 |
Correct |
1647 ms |
51880 KB |
Output is correct |
34 |
Correct |
1888 ms |
55236 KB |
Output is correct |
35 |
Correct |
1822 ms |
70680 KB |
Output is correct |
36 |
Correct |
1626 ms |
65544 KB |
Output is correct |
37 |
Correct |
1905 ms |
70792 KB |
Output is correct |