結果

問題 No.2114 01 Matching
ユーザー noshi91noshi91
提出日時 2022-10-28 23:09:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 585 ms / 5,000 ms
コード長 5,408 bytes
コンパイル時間 1,503 ms
コンパイル使用メモリ 108,000 KB
実行使用メモリ 54,912 KB
最終ジャッジ日時 2024-05-03 12:27:02
合計ジャッジ時間 16,414 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 194 ms
11,420 KB
testcase_03 AC 96 ms
14,208 KB
testcase_04 AC 124 ms
18,560 KB
testcase_05 AC 59 ms
6,692 KB
testcase_06 AC 186 ms
11,428 KB
testcase_07 AC 171 ms
6,784 KB
testcase_08 AC 185 ms
13,152 KB
testcase_09 AC 142 ms
7,476 KB
testcase_10 AC 197 ms
10,520 KB
testcase_11 AC 548 ms
54,784 KB
testcase_12 AC 555 ms
54,784 KB
testcase_13 AC 152 ms
14,752 KB
testcase_14 AC 194 ms
17,584 KB
testcase_15 AC 196 ms
17,580 KB
testcase_16 AC 222 ms
16,304 KB
testcase_17 AC 190 ms
11,424 KB
testcase_18 AC 110 ms
9,836 KB
testcase_19 AC 148 ms
14,856 KB
testcase_20 AC 553 ms
54,784 KB
testcase_21 AC 184 ms
15,440 KB
testcase_22 AC 116 ms
7,044 KB
testcase_23 AC 143 ms
10,624 KB
testcase_24 AC 41 ms
5,376 KB
testcase_25 AC 41 ms
6,280 KB
testcase_26 AC 556 ms
54,784 KB
testcase_27 AC 547 ms
54,784 KB
testcase_28 AC 277 ms
31,744 KB
testcase_29 AC 567 ms
54,784 KB
testcase_30 AC 171 ms
7,168 KB
testcase_31 AC 580 ms
54,784 KB
testcase_32 AC 185 ms
9,260 KB
testcase_33 AC 199 ms
11,544 KB
testcase_34 AC 171 ms
6,656 KB
testcase_35 AC 200 ms
24,448 KB
testcase_36 AC 585 ms
54,912 KB
testcase_37 AC 138 ms
9,052 KB
testcase_38 AC 173 ms
7,040 KB
testcase_39 AC 76 ms
6,188 KB
testcase_40 AC 173 ms
23,808 KB
testcase_41 AC 163 ms
8,040 KB
testcase_42 AC 130 ms
9,372 KB
testcase_43 AC 189 ms
11,436 KB
testcase_44 AC 207 ms
16,244 KB
testcase_45 AC 348 ms
54,656 KB
testcase_46 AC 53 ms
5,376 KB
testcase_47 AC 584 ms
54,784 KB
testcase_48 AC 574 ms
54,784 KB
testcase_49 AC 148 ms
14,852 KB
testcase_50 AC 71 ms
5,376 KB
testcase_51 AC 167 ms
6,772 KB
testcase_52 AC 194 ms
17,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


#include <iostream>
#include <map>
#include <vector>

//#define NDEBUG

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>

namespace n91 {

using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

struct rep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { ++i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr rep(const usize f, const usize l) noexcept
      : f(std::min(f, l)), l(l) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
struct revrep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { --i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr revrep(const usize f, const usize l) noexcept
      : f(l - 1), l(std::min(f, l) - 1) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
template <class T> auto md_vec(const usize n, const T &value) {
  return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
  return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) noexcept {
  return a < b ? b - a : a - b;
}
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class F> class rec_lambda {
  F f;

public:
  rec_lambda(F &&f) : f(std::move(f)) {}
  template <class... Args> auto operator()(Args &&... args) const {
    return f(*this, std::forward<Args>(args)...);
  }
};
template <class F> auto make_rec(F &&f) { return rec_lambda<F>(std::move(f)); }
template <class T> T scan() {
  T ret;
  std::cin >> ret;
  return ret;
}
constexpr char eoln = '\n';
template <class T> T ceildiv(const T &l, const T &r) {
  return l / r + (l % r != 0 ? 1 : 0);
}

} // namespace n91

#include <deque>

namespace n91 {

void main_() {

  const auto solve = [](std::vector<int> lxt, std::vector<int> rxt) -> i64 {
    if (lxt.empty()) {
      return 0;
    }
    struct vtx {
      int type; // 0:a, 1:b
      i64 v;
    };
    std::vector<vtx> vs;
    for (const auto e : lxt) {
      vs.push_back({0, e});
    }
    for (const auto e : rxt) {
      vs.push_back({1, e});
    }
    std::sort(vs.begin(), vs.end(),
              [](const auto &l, const auto &r) { return l.v < r.v; });

    struct aque {
      std::deque<std::pair<i64, i64>> v;
      i64 a;

      aque() : v(), a(0) {}

      void push_front(i64 x, i64 l) { v.push_front({x - a, l}); }
      void push_back(i64 x, i64 l) { v.push_back({x - a, l}); }
      bool empty() { return v.empty(); }
      i64 front() { return v.front().first + a; }
      std::pair<i64, i64> pop_front() {
        auto ret = v.front();
        v.pop_front();
        return {ret.first + a, ret.second};
      }
      std::pair<i64, i64> pop_back() {
        auto ret = v.back();
        v.pop_back();
        return {ret.first + a, ret.second};
      }
      void add(i64 x) { a += x; }
    };

    i64 min_pos = 0;
    i64 min_val = 0;
    aque m, r;
    r.push_back(1e10, 3e5);

    const auto shift = [&]() {
      auto [s, len] = m.pop_front();
      min_pos += len;
      min_val += s * len;
    };

    i64 prev = 0;
    for (const auto &[type, x] : vs) {
      const i64 dif = (x - prev);
      min_val += dif * (-min_pos);
      m.add(-dif);
      r.add(dif);

      while (!m.empty() && m.front() < 0) {
        shift();
      }

      if (type == 0) {
        min_pos -= 1;
        assert(!r.empty());
        auto [s, len] = r.pop_front();
        m.push_back(s, 1);
        if (len - 1 != 0) {
          r.push_front(s, len - 1);
        }
      } else {
        m.push_front(0, 1);
        i64 m_r = 1;
        while (m_r != 0) {
          assert(!m.empty());
          auto [s, len] = m.pop_back();
          const i64 t = std::min(len, m_r);
          r.push_front(s, t);
          m_r -= t;
          if (len - t != 0) {
            m.push_back(s, len - t);
          }
        }
      }
      prev = x;
    }

    while (!m.empty()) {
      shift();
    }

    return min_val;
  };

  int N, M, K;
  std::cin >> N >> M >> K;
  std::vector<int> B(N), R(M);
  for (auto &e : B) {
    std::cin >> e;
  }
  for (auto &e : R) {
    std::cin >> e;
  }

  if (N > M) {
    std::swap(N, M);
    std::swap(B, R);
  }

  std::map<int, std::pair<std::vector<int>, std::vector<int>>> map;
  for (const auto e : B) {
    map[e % K].first.push_back(e / K);
  }
  for (const auto e : R) {
    map[e % K].second.push_back(e / K);
  }

  long long ans = 0;
  for (auto &[key, val] : map) {
    auto &[l, r] = val;
    if (l.size() > r.size()) {
      std::cout << "-1\n";
      return;
    }
    ans += solve(l, r);
  }

  std::cout << ans << eoln;
}

} // namespace n91

int main() {
  n91::main_();
  return 0;
}
0