Submission #751581

# Submission time Handle Problem Language Result Execution time Memory
751581 2023-05-31T19:37:16 Z tch1cherin The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
3176 ms 102780 KB
#include <bits/stdc++.h>
using namespace std;

void solve() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> A(H, vector<int>(W));
  vector<pair<int, int>> P;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      P.emplace_back(i, j);
      cin >> A[i][j];
    } 
  }
  int ans = INT_MAX;
  for (int r = 0; r < 2; r++) {
    sort(P.begin(), P.end(), [&](pair<int, int> x, pair<int, int> y) {
      return A[x.first][x.second] < A[y.first][y.second];
    });
    vector<vector<int>> x(H, vector<int>(W + 1));
    for (int i = 0; i < H; i++) {
      x[i][W] = INT_MAX;
      for (int j = W - 1; j >= 0; j--) {
        x[i][j] = min(x[i][j + 1], A[i][j]);
      }
    }
    multiset<int> Spref, Ssuff;
    for (int i = 0; i < H; i++) {
      Spref.insert(x[i][0]);
      Ssuff.insert(x[i][0]);
    }
    vector<int> height(H), pref(H), suff(H);
    vector<vector<bool>> used(H, vector<bool>(W, false));
    ans = min(ans, A[P.back().first][P.back().second] - A[P[0].first][P[0].second]);
    for (int i = 0; i < H * W - 1; i++) {
      auto [u, v] = P[i];    
      used[u][v] = true;
      for (int j = height[u]; j < W; j++) {
        if (!used[u][j]) {
          break;
        }
        height[u]++;
      }
      int p = u == 0 ? INT_MAX : pref[u - 1];
      for (int j = u; j < H; j++) {
        p = min(p, height[j]);
        if (pref[j] == p) {
          break;
        }
        Spref.erase(Spref.find(x[j][pref[j]]));
        pref[j] = p;
        Spref.insert(x[j][pref[j]]);
      }
      int s = u == H - 1 ? INT_MAX : suff[u + 1];
      for (int j = u; j >= 0; j--) {
        s = min(s, height[j]);
        if (suff[j] == s) {
          break;
        }
        Ssuff.erase(Ssuff.find(x[j][suff[j]]));
        suff[j] = s;
        Ssuff.insert(x[j][suff[j]]);
      }
      ans = min(ans, max(A[u][v] - A[P[0].first][P[0].second], A[P.back().first][P.back().second] - max(*Spref.begin(), *Ssuff.begin())));
    }
    for (int i = 0; i < H; i++) {
      reverse(A[i].begin(), A[i].end());
    }
  }
  cout << ans << "\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 9 ms 1108 KB Output is correct
17 Correct 17 ms 1360 KB Output is correct
18 Correct 17 ms 1352 KB Output is correct
19 Correct 20 ms 1360 KB Output is correct
20 Correct 17 ms 1324 KB Output is correct
21 Correct 20 ms 1496 KB Output is correct
22 Correct 17 ms 1504 KB Output is correct
23 Correct 17 ms 1496 KB Output is correct
24 Correct 15 ms 1352 KB Output is correct
25 Correct 18 ms 1488 KB Output is correct
26 Correct 18 ms 1496 KB Output is correct
27 Correct 17 ms 1492 KB Output is correct
28 Correct 18 ms 1532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 9 ms 1108 KB Output is correct
17 Correct 17 ms 1360 KB Output is correct
18 Correct 17 ms 1352 KB Output is correct
19 Correct 20 ms 1360 KB Output is correct
20 Correct 17 ms 1324 KB Output is correct
21 Correct 20 ms 1496 KB Output is correct
22 Correct 17 ms 1504 KB Output is correct
23 Correct 17 ms 1496 KB Output is correct
24 Correct 15 ms 1352 KB Output is correct
25 Correct 18 ms 1488 KB Output is correct
26 Correct 18 ms 1496 KB Output is correct
27 Correct 17 ms 1492 KB Output is correct
28 Correct 18 ms 1532 KB Output is correct
29 Correct 2560 ms 82940 KB Output is correct
30 Correct 2309 ms 80760 KB Output is correct
31 Correct 2515 ms 87016 KB Output is correct
32 Correct 2698 ms 87084 KB Output is correct
33 Correct 1924 ms 75880 KB Output is correct
34 Correct 2556 ms 87080 KB Output is correct
35 Correct 3176 ms 102676 KB Output is correct
36 Correct 2375 ms 89524 KB Output is correct
37 Correct 3068 ms 102780 KB Output is correct