結果

問題 No.2157 崖
ユーザー atug tokyoatug tokyo
提出日時 2022-12-09 22:38:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,978 bytes
コンパイル時間 2,692 ms
コンパイル使用メモリ 205,048 KB
実行使用メモリ 19,380 KB
最終ジャッジ日時 2024-04-22 22:43:03
合計ジャッジ時間 49,089 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 4,824 ms
13,568 KB
testcase_14 AC 4,047 ms
18,048 KB
testcase_15 AC 4,768 ms
13,440 KB
testcase_16 AC 4,851 ms
13,600 KB
testcase_17 AC 3,450 ms
15,780 KB
testcase_18 AC 3,361 ms
15,360 KB
testcase_19 TLE -
testcase_20 AC 4,555 ms
19,380 KB
testcase_21 AC 4,431 ms
19,200 KB
testcase_22 AC 3,222 ms
14,856 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 5 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/fenwicktree>

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>;

const int INF = 1001001001;

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 ok = INF;
  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 == INF && !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