#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;
}
for(int j = 1; j <= last; j++) {
if(a[i][j] > mn + d) return 0;
}
maxi(last, r);
}
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:84:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
84 | mid = l + r >> 1;
| ~~^~~
joioi.cpp:90:11: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
90 | cout<<res;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2516 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2516 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2516 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |