Submission #1066184

#TimeUsernameProblemLanguageResultExecution timeMemory
1066184sammyuriThe Kingdom of JOIOI (JOI17_joioi)C++17
100 / 100
3764 ms106008 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...