結果

問題 No.2157 崖
ユーザー atug tokyoatug tokyo
提出日時 2022-12-10 01:54:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,035 bytes
コンパイル時間 2,508 ms
コンパイル使用メモリ 208,876 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2024-04-22 23:35:24
合計ジャッジ時間 9,408 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,948 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 WA -
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 WA -
testcase_12 AC 2 ms
6,944 KB
testcase_13 WA -
testcase_14 AC 506 ms
17,920 KB
testcase_15 AC 367 ms
13,440 KB
testcase_16 AC 370 ms
13,440 KB
testcase_17 AC 432 ms
15,744 KB
testcase_18 AC 424 ms
15,360 KB
testcase_19 WA -
testcase_20 AC 572 ms
19,456 KB
testcase_21 AC 568 ms
19,200 KB
testcase_22 AC 420 ms
14,976 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

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<Pair, vector<Pair>, greater<Pair>>;
using Table = VI2;
using Graph = VI2;

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;

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());
  }
  Table dp(N, VI1(M, INF));
  for (int i = 0; i < M; ++i) dp[0][i] = 0;
  for (int i = 0; i < N - 1; ++i) {
    auto &Di = D.at(i);
    PQ pq1, pq2;
    for (int j = 0, r = 0; j < M; ++j) {
      auto A2 = D[i + 1][j];
      for (; r < M; ++r) {
        if (A2 < Di[r]) break;
        pq1.emplace(dp[i][r], r);
        pq2.emplace(-Di[r], r);
      }
      while (pq1.size()) {
        auto [x, u] = pq1.top();
        if (A2 - Di[u] <= x) {
          chmin(dp[i + 1][j], x);
          break;
        }
        pq1.pop();
      }
      while (pq2.size()) {
        auto [x, u] = pq2.top();
        if (dp[i][u] <= A2 + x) {
          chmin(dp[i + 1][j], A2 + x);
          break;
        }
        pq2.pop();
      }
    }
  }
  auto min_cost = *min_element(dp[N - 1].begin(), dp[N - 1].end());
  if (min_cost == INF) return -1;
  return min_cost;
}

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