結果

問題 No.2157 崖
ユーザー atug tokyoatug tokyo
提出日時 2022-12-09 23:10:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,985 bytes
コンパイル時間 2,265 ms
コンパイル使用メモリ 210,276 KB
実行使用メモリ 19,500 KB
最終ジャッジ日時 2023-08-05 01:03:30
合計ジャッジ時間 50,397 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 4,854 ms
13,532 KB
testcase_14 AC 4,144 ms
17,800 KB
testcase_15 AC 4,786 ms
13,396 KB
testcase_16 AC 4,901 ms
13,564 KB
testcase_17 AC 3,507 ms
15,720 KB
testcase_18 AC 4,564 ms
15,472 KB
testcase_19 TLE -
testcase_20 AC 4,675 ms
19,500 KB
testcase_21 AC 4,605 ms
19,228 KB
testcase_22 AC 3,285 ms
14,980 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/fenwicktree>
#include <atcoder/segtree>

using namespace std;
using ll = long long;
using ld = long double;

using Pair = pair<int, int>;
using Tuple = tuple<int, int, int>;
using VI1 = vector<int>;
using VI2 = vector<VI1>;
using VL1 = vector<ll>;
using VL2 = vector<VL1>;
using VD1 = vector<ld>;
using VD2 = vector<VD1>;
using VB1 = vector<bool>;
using VB2 = vector<VB1>;
using VP1 = vector<Pair>;
using VP2 = vector<VP1>;
using VT1 = vector<Tuple>;
using VT2 = vector<VT1>;

using Queue = queue<int>;
using DQ = deque<int>;
using PQ = priority_queue<int, vector<int>, greater<int>>;
using Table = VI2;
using Graph = VI2;
using FW = atcoder::fenwick_tree<int>;

int op(int a, int b) { return max(a, b); }
int e() { return 0; }
using Seg = atcoder::segtree<int, op, e>;

template <typename T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 1001001001;

template <typename T>
vector<T> compress(vector<T> &org, vector<int> &compressed) {
  int size = org.size();
  vector<T> values(org);
  sort(values.begin(), values.end());
  values.erase(unique(values.begin(), values.end()), values.end());
  compressed.resize(size);
  for (int i = 0; i < size; ++i) {
    auto oi = org.at(i);
    auto it = lower_bound(values.begin(), values.end(), oi);
    compressed.at(i) = it - values.begin();
  }
  return values;
}

bool is_ok(int N, int M, Table &D, int mid) {
  Table dp(N, VI1(M, 0));
  for (int i = 0; i < M; ++i) dp[0][i] = 1;
  for (int i = 0; i < N - 1; ++i) {
    auto &Di = D.at(i);
    FW fw(M);
    for (int j = 0; j < M; ++j) {
      if (dp[i][j] == 1) fw.add(j, 1);
    }
    for (int j = 0; j < M; ++j) {
      auto A2 = D[i + 1][j];
      auto lb = A2 - mid, ub = A2;
      auto it1 = lower_bound(Di.begin(), Di.end(), lb);
      auto it2 = upper_bound(Di.begin(), Di.end(), ub);
      if (it1 == Di.end()) continue;
      int l = it1 - Di.begin(), r = it2 - Di.begin();
      if (fw.sum(l, r) > 0) dp[i + 1][j] = 1;
    }
  }
  auto max_dp = *max_element(dp[N - 1].begin(), dp[N - 1].end());
  return (max_dp == 1);
}

auto solve() {
  int N, M;
  cin >> N >> M;
  Table D(N, VI1(M));
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) cin >> D[i][j];
    sort(D.at(i).begin(), D.at(i).end());
  }
  int min_D = INF, max_D = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      chmin(min_D, D[i][j]);
      chmax(max_D, D[i][j]);
    }
  }
  int ok = max_D - min_D + 1;
  int ng = -1;
  while (abs(ok - ng) > 1) {
    auto mid = (ok + ng) / 2;
    if (is_ok(N, M, D, mid))
      ok = mid;
    else
      ng = mid;
  }
  if (ok == max_D - min_D + 1 && !is_ok(N, M, D, ok)) return -1;
  return ok;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  auto result = solve();
  cout << result << endl;
}
0