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>
using namespace std;
const int nax = 2001;
const int max_a = 1000000000;
int mat[nax][nax];
bool curr[nax][nax];
int mn = max_a, mx = 0;
int n, m;
void print() {
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cout << mat[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
}
void mini(int& a, int b) {
a = min(a, b);
}
bool gali(int k) {
int idx = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
// max priklauso [mx-k, mx]
if (mx - k > mat[i][j]) {
// (i, j) butinai priklauso TIK minimumams
idx = max(idx, j+1);
}
}
// visi tarp tures buti minimumai [mn, mn+k]
for(int j = 0; j < idx; j++) {
if (mat[i][j] > mn + k) {
return false;
}
}
}
return true;
}
void flip1() {
int aux[m][n];
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
aux[i][j] = mat[j][m-i-1];
//cout << i << " " << j << ": " << j << " " << n-i-1 << " " << aux[i][j] << "\n";
}
}
swap(n, m);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++){
mat[i][j] = aux[i][j];
}
}
}
int go() {
int l = 0, r = max_a, ans = max_a;
while(l <= r) {
int mid = (l + r) / 2;
if (gali(mid)) {
ans = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> mat[i][j];
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
mn = min(mn, mat[i][j]);
mx = max(mx, mat[i][j]);
}
}
//cout << mn << " " << mx << "\n";
int ans = go();
flip1();
//print();
mini(ans, go());
flip1();
//print();
mini(ans, go());
flip1();
//print();
mini(ans, go());
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |