이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int height[4][2005][2005];
bool taken[2005][2005];
int biggest = 0;
int h, w;
bool check(int limit, int index) {
// take all cells that are big enough
int req = biggest - limit;
int curbig = 0, cursmall = 1e9;
for (int i = 0; i < h; i ++)
for (int j = 0; j < w; j ++)
if (height[index][i][j] < req)
taken[i][j] = true;
else
taken[i][j] = false;
// turn into one component
for (int i = 0; i < h; i ++) {
bool seen = false;
for (int j = w - 1; j >= 0; j --) {
if (taken[i][j]) seen = true;
if (seen) {
taken[i][j] = true;
curbig = max(curbig, height[index][i][j]);
cursmall = min(cursmall, height[index][i][j]);
}
}
}
for (int j = 0; j < w; j ++) {
bool seen = false;
for (int i = h - 1; i >= 0; i --) {
if (taken[i][j]) seen = true;
if (seen) {
taken[i][j] = true;
curbig = max(curbig, height[index][i][j]);
cursmall = min(cursmall, height[index][i][j]);
}
}
}
return (curbig - cursmall) <= limit;
}
bool possible(int limit) {
if (check(limit, 0))
return true;
if (check(limit, 1))
return true;
if (check(limit, 2))
return true;
if (check(limit, 3))
return true;
return false;
}
int main() {
cin >> h >> w;
for (int i = 0; i < h; i ++) {
for (int j = 0; j < w; j ++)
cin >> height[0][i][j], biggest = max(biggest, height[0][i][j]);
}
for (int i = 0; i < h; i ++)
for (int j = 0; j < w; j ++)
height[1][i][j] = height[0][h - i - 1][j],
height[2][i][j] = height[0][i][w - j - 1],
height[3][i][j] = height[0][h - i - 1][w - j - 1];
int L = 0, R = 1e9 + 5;
while (L != R) {
int mid = (L + R) / 2;
if (possible(mid))
R = mid;
else
L = mid + 1;
}
cout << L << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |