//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, b, a) for (int i = b; i >= a; --i)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define lsb(x) (x)&(-x)
#define bug(x) cerr << #x << " " << x << endl;
void setIO(string name = "") {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
ll fexp(ll a, ll b, ll m) {
if (b == 0) return 1LL;
ll p = a;
ll ans = 1;
while (b > 0) {
if (b % 2 != 0) ans = (ans * p) % m;
p = (p * p) % m;
b >>= 1;
}
return ans;
}
const int MAXN = 2e3 + 10;
const int INF = 1e18 + 5;
const int MOD = 1e9 + 7;
int a[MAXN][MAXN], mk[MAXN][MAXN],n,m;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
void dfs(int i, int j, int K,int to=1,int mn=-INF) {
if (mk[i][j]==to) return;
mk[i][j] = to;
rep(k,0,3) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < 1 || nx > n || ny < 1 || ny > m || a[nx][ny] > K || a[nx][ny] < mn) continue;
dfs(nx,ny,K,to,mn);
}
}
void solve() {
cin >> n >> m;
rep(i,1,n) rep(j,1,m) cin >> a[i][j];
int mi = 1, mj = 1;
rep(i,1,n) rep(j,1,m) if (a[i][j] < a[mi][mj]) mi = i, mj = j;
int lo = 0, hi = 1e9, ans=-1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
rep(i,1,n) rep(j,1,m) mk[i][j] = 0;
dfs(mi,mj,a[mi][mj]+mid);
int x = 0,y = 0, mn = INF, mx =-INF;
bool ok = 1;
rep(i,1,n) rep(j,1,m) if (!mk[i][j]) x = i, y = j, mn = min(mn,a[i][j]), mx = max(mx,a[i][j]);
if(x > 0) dfs(x,y,mx, 2, mx-mid+1);
rep(i,1,n) rep(j,1,m) ok &= (mk[i][j]!=0);
if (mx-mn<=mid && ok) {
hi = mid-1;
ans = mid;
} else {
lo = mid + 1;
}
}
cout << ans << '\n';
}
int32_t main() {
setIO();
int tt = 1;
//cin >> tt;
while (tt-- > 0) solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
joioi.cpp: In function 'void setIO(std::string)':
joioi.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:30:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen((name + ".out").c_str(), "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... |