This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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++) {
if(ok(d)) return 1;
flip();
}
return 0;
}
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 = mx - mn, 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 (stderr)
joioi.cpp: In function 'bool check(int)':
joioi.cpp:55:9: warning: unused variable 'ans' [-Wunused-variable]
55 | int ans = 0;
| ^~~
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;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |