Submission #321665

# Submission time Handle Problem Language Result Execution time Memory
321665 2020-11-13T02:55:07 Z casperwang The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
858 ms 117740 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2000;
const int INF = 1e9;
int n, m;
int a[MAXN+1][MAXN+1];
int tmp[MAXN+1][MAXN+1];
int pre[MAXN+1][MAXN+1];
int suf_mmax[MAXN+2][MAXN+2];
int suf_mmin[MAXN+2][MAXN+2];
int R[MAXN+1];
int mmin;
int ans;

bool solve(int n, int m, int val) {
  for (int i = 1; i <= n; i++) {
    R[i] = upper_bound(pre[i]+1, pre[i]+1+m, mmin+val) - pre[i] - 1;
    if (i > 1) R[i] = min(R[i], R[i-1]);
  }
  if (!R[1]) return false;
  int _mmax = 0, _mmin = INF;
  for (int i = 1; i <= n; i++) {
    _mmax = max(_mmax, suf_mmax[i][R[i]+1]);
    _mmin = min(_mmin, suf_mmin[i][R[i]+1]);
  }
  if (_mmax - _mmin > val) return false;
  return true;
}

int cal(int n, int m) {
  int l = 0, r = 1e9, mid;
  while (l < r) {
    mid = l + r >> 1;
    if (!solve(n, m, mid))
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}

void rotate(int n, int m, bool flag) {
  int N = max(n, m);
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++)
      tmp[i][j] = a[j][i];
  if (!flag) {
    for (int i = 1; i <= m; i++)
      for (int j = 1; j <= n/2; j++)
        swap(tmp[i][j], tmp[i][n-j+1]);
  }
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++)
      a[i][j] = tmp[i][j];
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      pre[i][j] = max({a[i][j], pre[i-1][j], pre[i][j-1]});
  for (int i = 1; i <= N+1; i++) {
    for (int j = 1; j <= N+1; j++) {
      suf_mmin[i][j] = INF;
      suf_mmax[i][j] = 0;
    }
  }
  for (int i = m; i >= 1; i--) {
    for (int j = n; j >= 1; j--) {
      suf_mmin[i][j] = min({a[i][j], suf_mmin[i+1][j], suf_mmin[i][j+1]});
      suf_mmax[i][j] = max({a[i][j], suf_mmax[i+1][j], suf_mmax[i][j+1]});
    }
  }
  return;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++)
      cout << a[i][j] << " \n"[j==N];
  cout << "\n";
}

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  mmin = INF;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      mmin = min(mmin, a[i][j]);
    }
  }
  ans = INF;
  rotate(n, m, true);
  ans = min(ans, cal(m, n));
  rotate(m, n, false);
  ans = min(ans, cal(n, m));
  rotate(n, m, false);
  ans = min(ans, cal(m, n));
  rotate(m, n, false);
  ans = min(ans, cal(n, m));
  cout << ans << "\n";
}

Compilation message

joioi.cpp: In function 'int cal(int, int)':
joioi.cpp:34:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   34 |     mid = l + r >> 1;
      |           ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 620 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 620 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 1 ms 620 KB Output is correct
9 Correct 1 ms 620 KB Output is correct
10 Correct 1 ms 620 KB Output is correct
11 Correct 1 ms 620 KB Output is correct
12 Correct 1 ms 620 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 620 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 620 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 1 ms 620 KB Output is correct
9 Correct 1 ms 620 KB Output is correct
10 Correct 1 ms 620 KB Output is correct
11 Correct 1 ms 620 KB Output is correct
12 Correct 1 ms 620 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
15 Correct 4 ms 4972 KB Output is correct
16 Correct 8 ms 5100 KB Output is correct
17 Correct 9 ms 5100 KB Output is correct
18 Correct 9 ms 5100 KB Output is correct
19 Correct 10 ms 5100 KB Output is correct
20 Correct 9 ms 5100 KB Output is correct
21 Correct 10 ms 5100 KB Output is correct
22 Correct 10 ms 5100 KB Output is correct
23 Correct 10 ms 5100 KB Output is correct
24 Correct 9 ms 5100 KB Output is correct
25 Correct 10 ms 5100 KB Output is correct
26 Correct 10 ms 5100 KB Output is correct
27 Correct 10 ms 5100 KB Output is correct
28 Correct 10 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 620 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 620 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 1 ms 620 KB Output is correct
9 Correct 1 ms 620 KB Output is correct
10 Correct 1 ms 620 KB Output is correct
11 Correct 1 ms 620 KB Output is correct
12 Correct 1 ms 620 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
15 Correct 4 ms 4972 KB Output is correct
16 Correct 8 ms 5100 KB Output is correct
17 Correct 9 ms 5100 KB Output is correct
18 Correct 9 ms 5100 KB Output is correct
19 Correct 10 ms 5100 KB Output is correct
20 Correct 9 ms 5100 KB Output is correct
21 Correct 10 ms 5100 KB Output is correct
22 Correct 10 ms 5100 KB Output is correct
23 Correct 10 ms 5100 KB Output is correct
24 Correct 9 ms 5100 KB Output is correct
25 Correct 10 ms 5100 KB Output is correct
26 Correct 10 ms 5100 KB Output is correct
27 Correct 10 ms 5100 KB Output is correct
28 Correct 10 ms 5100 KB Output is correct
29 Correct 722 ms 78912 KB Output is correct
30 Correct 718 ms 100332 KB Output is correct
31 Correct 750 ms 101868 KB Output is correct
32 Correct 747 ms 101868 KB Output is correct
33 Correct 687 ms 98320 KB Output is correct
34 Correct 745 ms 102124 KB Output is correct
35 Correct 858 ms 117660 KB Output is correct
36 Correct 743 ms 112160 KB Output is correct
37 Correct 834 ms 117740 KB Output is correct