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 <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#define sz(x) int(x.size())
using vi = vector<int>;
using vvi = vector<vi>;
vvi T;
int lim;
//1 = lo, 2 = hi, 3 = both
bool solve()
{
int H = sz(T) - 2;
int W = sz(T[0]) - 2;
bool works = 1;
vvi P = T;
for(int i = 1; i <= H; i++)
{
for(int j = 1; j <= W; j++)
{
P[i][j] = (P[i-1][j] || P[i][j-1] || (T[i][j] == 1));
if(P[i][j] && (T[i][j] == 2))
works = 0;
}
}
return works;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int H, W;
cin >> H >> W;
int res = 2'000'000'001;
int amx = 0, amn = 1'000'000'001;
vvi A = vvi(1+H+1, vi(1+W+1));
for(int i = 1; i <= H; i++)
{
for(int j = 1; j <= W; j++)
{
cin >> A[i][j];
amx = max(amx, A[i][j]);
amn = min(amn, A[i][j]);
}
}
T = vvi(1+H+1, vi(1+W+1));
int lo = 0, hi = 1'000'000'000;
while(lo != hi)
{
lim = (lo+hi)/2;
bool works = 0;
bool failed = 0;
for(int i = 1; i <= H; i++)
{
for(int j = 1; j <= W; j++)
{
int islo = (A[i][j] - amn) <= lim;
int ishi = (amx - A[i][j]) <= lim;
if(islo && ishi)
T[i][j] = 3;
else if(islo)
T[i][j] = 1;
else if(ishi)
T[i][j] = 2;
else
failed = 1;
}
}
if(failed)
{
works = 0;
}
else
{
works = 0;
works |= solve();
for(int i = 1; i <= H; i++)
reverse(T[i].begin(), T[i].end());
works |= solve();
for(int j = 1; j <= W; j++)
for(int i = 1; 2*i <= H; i++)
swap(T[i][j], T[H-i+1][j]);
works |= solve();
for(int i = 1; i <= H; i++)
reverse(T[i].begin(), T[i].end());
works |= solve();
}
if(works)
hi = lim;
else
lo = lim+1;
}
cout << lo << '\n';
}
Compilation message (stderr)
joioi.cpp: In function 'int main()':
joioi.cpp:46:6: warning: unused variable 'res' [-Wunused-variable]
46 | int res = 2'000'000'001;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |