#include<bits/stdc++.h>
using namespace std;
void local() {
#define taskname ""
if(fopen(taskname".inp", "r")) {
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
}
#define ll long long
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
template <class X, class Y> bool mini(X& x, Y y) {return x > y ? x = y, true : false;}
template <class X, class Y> bool maxi(X& x, Y y) {return x < y ? x = y, true : false;}
const int N = 2e3 + 5;
int n, m;
vector<vector<int>> a;
int h[N], sf[N];
bool pls_check_2(int x, bool t) {
int ma = -2e9, mi = 2e9;
if(t) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
a[i][j] = -a[i][j];
}
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
maxi(ma, a[i][j]);
mini(mi, a[i][j]);
}
}
/* sf[n + 1] = n;
for(int i = n; i > 0; i--) {
sf[i] = 0;
for(int j = 1; j <= m; j++) {
if(a[i][j] == mi) break;
sf[i] = j;
}
mini(sf[i], sf[i + 1]);
}*/
h[n + 1] = m;
for(int i = n; i > 0; i--) {
h[i] = 0;
for(int j = 1; j <= m; j++) {
if(ma - a[i][j] > x) break;
h[i] = j;
}
if(i == 1) h[i] = min(h[i], m - 1);
mini(h[i], h[i + 1]);
}
ma = -2e9, mi = 2e9;
for(int i = 1; i <= n; i++) {
for(int j = h[i] + 1; j <= m; j++) {
maxi(ma, a[i][j]);
mini(mi, a[i][j]);
}
}
if(t) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
a[i][j] = -a[i][j];
}
}
}
return ma == -2e9 || ma - mi <= x;
}
bool pls_check(int x, bool t) {
if(t) {
for(int i = 1; i * 2 <= n; i++) {
swap(a[i], a[n - i + 1]);
}
}
bool ok = pls_check_2(x, 0) || pls_check_2(x, 1);
if(t) {
for(int i = 1; i * 2 <= n; i++) {
swap(a[i], a[n - i + 1]);
}
}
return ok;
}
bool check(int x) {
return pls_check(x, 0) || pls_check(x, 1);
}
int main() {
fastio; local();
cin >> n >> m;
a.resize(n + 5, vector<int>(m + 5));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
int lo = 0, hi = 1e9, res = -1;
while(lo <= hi) {
int mid = (lo + hi) >> 1;
if(check(mid)) hi = (res = mid) - 1;
else lo = mid + 1;
}
cout << res;
}
컴파일 시 표준 에러 (stderr) 메시지
joioi.cpp: In function 'void local()':
joioi.cpp:7:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
7 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:8:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |