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;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2005;
int n, m;
vector<vector<int> > a(N, vector<int> (N));
int maxVal = 0, minVal = INT_MAX;
void FlipRows() {
for(int i = 1; i <= n / 2; i++) {
for(int j = 1; j <= m; j++) {
swap(a[i][j], a[n - i + 1][j]);
}
}
}
void FlipColumns() {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m / 2; j++) {
swap(a[i][j], a[i][m - j + 1]);
}
}
}
bool Check(int max_delta) {
int max_row = n;
//cout << "max_delta = " << max_delta << "\n";
for(int j = 1; j <= m; j++) {
for(int i = 1; i <= max_row; i++) {
if(maxVal - a[i][j] > max_delta) {
max_row = i - 1;
//cout << "In column " << j << ", the row max is being reduced to: " << i - 1 << "\n";
break;
}
}
for(int i = max_row + 1; i <= n; i++) {
if(a[i][j] - minVal > max_delta) {
//cout << "a[i][j] - minVal = " << a[i][j] - minVal << ", max_delta = " << max_delta << "\n";
//cout << "a[i][j] = " << a[i][j] << ", minVal = " << minVal << "\n";
//cout << "Exceeding the limits...can't take anymore\n";
//cout << "Place = (" << i << ", " << j << ")\n";
return false;
}
}
}
return true;
}
int FindVal() {
int l = 0, r = maxVal - minVal + 1;
int mid, ans = r;
while(l <= r) {
mid = (l + r) >> 1;
if(Check(mid)) {
ans = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
//cout << "ans = " << ans << "\n";
return ans;
}
void Solve() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i][j];
maxVal = max(maxVal, a[i][j]);
minVal = min(minVal, a[i][j]);
}
}
/*cout << "maxVal = " << maxVal << ", minVal = " << minVal << "\n";
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}*/
int ans = FindVal();
FlipColumns();
/*for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}*/
ans = min(ans, FindVal());
FlipRows();
/*for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}*/
ans = min(ans, FindVal());
FlipColumns();
/*for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}*/
cout << min(ans, FindVal()) << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |