Submission #837209

# Submission time Handle Problem Language Result Execution time Memory
837209 2023-08-25T08:09:12 Z _martynas The Kingdom of JOIOI (JOI17_joioi) C++11
100 / 100
1328 ms 20044 KB
#include <bits/stdc++.h>

using namespace std;

#warning change mxn
const int mxn = 2005;
const int inf = 2e9;

int h, w;
int mxa = 0;
int a[mxn][mxn];
bool b[mxn][mxn];

int check() {
    int mnj = w;
    int mn = inf/2, mx = 0;
    for(int i = 0; i < h; i++) {
        for(int j = 0; j < mnj; j++) {
            if(b[i][j]) mnj = j;
        }
        for(int j = mnj; j < w; j++) {
            mn = min(mn, a[i][j]);
            mx = max(mx, a[i][j]);
        }
    }
    return mx-mn;
}

bool solve(int k) {
    for(int i = 0; i < h; i++)
    for(int j = 0; j < w; j++)
        b[i][j] = mxa-a[i][j]>k;
    bool ok = false;
    // check
    ok = ok || (check() <= k);
    // reverse rows
    for(int i = 0; i < h; i++) for(int j = 0; j < w/2; j++) swap(a[i][j], a[i][w-j-1]);
    for(int i = 0; i < h; i++) for(int j = 0; j < w/2; j++) swap(b[i][j], b[i][w-j-1]);
    // check
    ok = ok || (check() <= k);
    // reverse cols
    for(int i = 0; i < h/2; i++) for(int j = 0; j < w; j++) swap(a[i][j], a[h-i-1][j]);
    for(int i = 0; i < h/2; i++) for(int j = 0; j < w; j++) swap(b[i][j], b[h-i-1][j]);
    // check
    ok = ok || (check() <= k);
    // reverse rows
    for(int i = 0; i < h; i++) for(int j = 0; j < w/2; j++) swap(a[i][j], a[i][w-j-1]);
    for(int i = 0; i < h; i++) for(int j = 0; j < w/2; j++) swap(b[i][j], b[i][w-j-1]);
    // check
    ok = ok || (check() <= k);
    // reverse cols
    for(int i = 0; i < h/2; i++) for(int j = 0; j < w; j++) swap(a[i][j], a[h-i-1][j]);
    for(int i = 0; i < h/2; i++) for(int j = 0; j < w; j++) swap(b[i][j], b[h-i-1][j]);
    return ok;
}

int main(int argc, char const *argv[]) {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> h >> w;
    for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++) {
            cin >> a[i][j]; mxa = max(mxa, a[i][j]);
        }
    }
    int lo = 0, hi = inf/2;
    while(lo < hi) {
        int m = (lo+hi)/2;
        if(solve(m)) {
            hi = m;
        }
        else {
            lo = m+1;
        }
    }
    cout << lo << "\n";
    return 0;
}

Compilation message

joioi.cpp:5:2: warning: #warning change mxn [-Wcpp]
    5 | #warning change mxn
      |  ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 272 KB Output is correct
4 Correct 1 ms 368 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 272 KB Output is correct
4 Correct 1 ms 368 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 8 ms 1620 KB Output is correct
17 Correct 11 ms 1620 KB Output is correct
18 Correct 12 ms 1676 KB Output is correct
19 Correct 11 ms 1584 KB Output is correct
20 Correct 10 ms 1524 KB Output is correct
21 Correct 14 ms 1680 KB Output is correct
22 Correct 15 ms 1604 KB Output is correct
23 Correct 14 ms 1620 KB Output is correct
24 Correct 12 ms 1484 KB Output is correct
25 Correct 14 ms 1676 KB Output is correct
26 Correct 13 ms 1632 KB Output is correct
27 Correct 13 ms 1620 KB Output is correct
28 Correct 13 ms 1620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 272 KB Output is correct
4 Correct 1 ms 368 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 8 ms 1620 KB Output is correct
17 Correct 11 ms 1620 KB Output is correct
18 Correct 12 ms 1676 KB Output is correct
19 Correct 11 ms 1584 KB Output is correct
20 Correct 10 ms 1524 KB Output is correct
21 Correct 14 ms 1680 KB Output is correct
22 Correct 15 ms 1604 KB Output is correct
23 Correct 14 ms 1620 KB Output is correct
24 Correct 12 ms 1484 KB Output is correct
25 Correct 14 ms 1676 KB Output is correct
26 Correct 13 ms 1632 KB Output is correct
27 Correct 13 ms 1620 KB Output is correct
28 Correct 13 ms 1620 KB Output is correct
29 Correct 1092 ms 18956 KB Output is correct
30 Correct 1018 ms 19932 KB Output is correct
31 Correct 1098 ms 19916 KB Output is correct
32 Correct 1101 ms 19936 KB Output is correct
33 Correct 994 ms 17580 KB Output is correct
34 Correct 1081 ms 19928 KB Output is correct
35 Correct 1328 ms 20044 KB Output is correct
36 Correct 1292 ms 19880 KB Output is correct
37 Correct 1246 ms 19924 KB Output is correct