답안 #207811

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
207811 2020-03-09T05:24:25 Z tri The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
2453 ms 258400 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

const int MAXN = 2000 + 10;

int H, W;

int grid[MAXN][MAXN];
int taken[MAXN][MAXN];

inline int lin(int i, int j) {
    return i * W + j;
}

inline pi dLin(int x) {
    return {x / W, x % W};
}

inline bool check(int i, int j) {
    if (i - 1 >= 0 && !taken[i - 1][j]) {
        return false;
    }
    if (j - 1 >= 0 && !taken[i][j - 1]) {
        return false;
    }
    return true;
}

int part1[MAXN * MAXN];
vi rem[MAXN * MAXN];

// compute min cost assuming TL-BR groups and that TL group is smaller
int compute() {
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            taken[i][j] = 0;
        }
    }
    vi nums;

    int cur = grid[0][0];

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (grid[i][j] >= cur) {
                nums.pb(grid[i][j]);
            }
        }
    }

    sort(nums.begin(), nums.end());
    nums.erase(unique(nums.begin(), nums.end()), nums.end());

    priority_queue<pi, vector<pi>, greater<pi>> q;

    q.push({cur, lin(0, 0)});

    int cMin = cur;
    int cMax = cur;

    int ans = 1e9;

    int nStep = 0;

    for (int uBI = 0; uBI < nums.size(); uBI++) {
        int uB = nums[uBI];

        rem[nStep].clear();

        while (q.size()) {
            pi nxt = q.top();
            if (nxt.f > uB) {
                break;
            }
            q.pop();

            int x = nxt.s;
            pi coor = dLin(x);

            int cV = grid[coor.f][coor.s];

            rem[nStep].pb(cV);

            cMin = min(cMin, cV);
            cMax = max(cMax, cV);

            taken[coor.f][coor.s] = true;

            if (coor.f + 1 < H && check(coor.f + 1, coor.s)) {
                q.push({grid[coor.f + 1][coor.s], lin(coor.f + 1, coor.s)});
            }
            if (coor.s + 1 < W && check(coor.f, coor.s + 1)) {
                q.push({grid[coor.f][coor.s + 1], lin(coor.f, coor.s + 1)});
            }
        }

        int cost = cMax - cMin;
        part1[nStep++] = cost;
    }

    int min2 = 1e9;
    int max2 = 0;

    for (int cStep = nStep - 1; cStep >= 0; cStep--) {
        if (cStep != nStep - 1) {
            ans = min(ans, max(max2 - min2, part1[cStep]));
        }
        for(int x : rem[cStep]){
            min2 = min(min2, x);
            max2 = max(max2, x);
        }
    }

    return ans;
}

void flipMag() {
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            grid[i][j] = 1000000001 - grid[i][j];
        }
    }
}

void flip() {
    for (int j = 0; j < W; j++) {
        for (int i = 0; i < H / 2; i++) {
            int tmp = grid[i][j];
            grid[i][j] = grid[H - 1 - i][j];
            grid[H - 1 - i][j] = tmp;
        }
    }
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> H >> W;

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> grid[i][j];
        }
    }

    int ans = compute();

    flipMag();
    int ans2 = compute();

    flip();
    int ans3 = compute();

    flipMag();
    int ans4 = compute();

    int fAns = min(min(ans, ans2), min(ans3, ans4));
    cout << fAns << endl;
}

Compilation message

joioi.cpp: In function 'int compute()':
joioi.cpp:79:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int uBI = 0; uBI < nums.size(); uBI++) {
                       ~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 95224 KB Output is correct
2 Correct 60 ms 95352 KB Output is correct
3 Correct 59 ms 95352 KB Output is correct
4 Correct 75 ms 95352 KB Output is correct
5 Correct 60 ms 95352 KB Output is correct
6 Correct 59 ms 95228 KB Output is correct
7 Correct 59 ms 95352 KB Output is correct
8 Correct 61 ms 95352 KB Output is correct
9 Correct 62 ms 95352 KB Output is correct
10 Correct 63 ms 95352 KB Output is correct
11 Correct 61 ms 95352 KB Output is correct
12 Correct 60 ms 95352 KB Output is correct
13 Correct 62 ms 95352 KB Output is correct
14 Correct 59 ms 95352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 95224 KB Output is correct
2 Correct 60 ms 95352 KB Output is correct
3 Correct 59 ms 95352 KB Output is correct
4 Correct 75 ms 95352 KB Output is correct
5 Correct 60 ms 95352 KB Output is correct
6 Correct 59 ms 95228 KB Output is correct
7 Correct 59 ms 95352 KB Output is correct
8 Correct 61 ms 95352 KB Output is correct
9 Correct 62 ms 95352 KB Output is correct
10 Correct 63 ms 95352 KB Output is correct
11 Correct 61 ms 95352 KB Output is correct
12 Correct 60 ms 95352 KB Output is correct
13 Correct 62 ms 95352 KB Output is correct
14 Correct 59 ms 95352 KB Output is correct
15 Correct 61 ms 95352 KB Output is correct
16 Correct 72 ms 98272 KB Output is correct
17 Correct 81 ms 98276 KB Output is correct
18 Correct 80 ms 98268 KB Output is correct
19 Correct 83 ms 98192 KB Output is correct
20 Correct 80 ms 98164 KB Output is correct
21 Correct 84 ms 98264 KB Output is correct
22 Correct 84 ms 98368 KB Output is correct
23 Correct 79 ms 98132 KB Output is correct
24 Correct 82 ms 98040 KB Output is correct
25 Correct 82 ms 98252 KB Output is correct
26 Correct 81 ms 98188 KB Output is correct
27 Correct 82 ms 98264 KB Output is correct
28 Correct 83 ms 98212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 95224 KB Output is correct
2 Correct 60 ms 95352 KB Output is correct
3 Correct 59 ms 95352 KB Output is correct
4 Correct 75 ms 95352 KB Output is correct
5 Correct 60 ms 95352 KB Output is correct
6 Correct 59 ms 95228 KB Output is correct
7 Correct 59 ms 95352 KB Output is correct
8 Correct 61 ms 95352 KB Output is correct
9 Correct 62 ms 95352 KB Output is correct
10 Correct 63 ms 95352 KB Output is correct
11 Correct 61 ms 95352 KB Output is correct
12 Correct 60 ms 95352 KB Output is correct
13 Correct 62 ms 95352 KB Output is correct
14 Correct 59 ms 95352 KB Output is correct
15 Correct 61 ms 95352 KB Output is correct
16 Correct 72 ms 98272 KB Output is correct
17 Correct 81 ms 98276 KB Output is correct
18 Correct 80 ms 98268 KB Output is correct
19 Correct 83 ms 98192 KB Output is correct
20 Correct 80 ms 98164 KB Output is correct
21 Correct 84 ms 98264 KB Output is correct
22 Correct 84 ms 98368 KB Output is correct
23 Correct 79 ms 98132 KB Output is correct
24 Correct 82 ms 98040 KB Output is correct
25 Correct 82 ms 98252 KB Output is correct
26 Correct 81 ms 98188 KB Output is correct
27 Correct 82 ms 98264 KB Output is correct
28 Correct 83 ms 98212 KB Output is correct
29 Correct 2008 ms 231292 KB Output is correct
30 Correct 1996 ms 241744 KB Output is correct
31 Correct 2086 ms 242268 KB Output is correct
32 Correct 2114 ms 241472 KB Output is correct
33 Correct 1831 ms 230544 KB Output is correct
34 Correct 2110 ms 252516 KB Output is correct
35 Correct 2453 ms 247208 KB Output is correct
36 Correct 2138 ms 248116 KB Output is correct
37 Correct 2453 ms 258400 KB Output is correct