結果

問題 No.2501 Maximum Inversion Number
ユーザー 👑 emthrmemthrm
提出日時 2023-07-13 01:04:24
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 536 ms / 2,000 ms
コード長 2,832 bytes
コンパイル時間 1,584 ms
コンパイル使用メモリ 131,668 KB
実行使用メモリ 7,112 KB
最終ジャッジ日時 2023-10-13 18:04:40
合計ジャッジ時間 5,875 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 108 ms
4,356 KB
testcase_02 AC 197 ms
4,352 KB
testcase_03 AC 149 ms
4,348 KB
testcase_04 AC 136 ms
6,984 KB
testcase_05 AC 140 ms
7,096 KB
testcase_06 AC 146 ms
7,112 KB
testcase_07 AC 148 ms
4,608 KB
testcase_08 AC 145 ms
4,464 KB
testcase_09 AC 81 ms
4,980 KB
testcase_10 AC 113 ms
4,348 KB
testcase_11 AC 104 ms
4,400 KB
testcase_12 AC 109 ms
4,372 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 166 ms
6,968 KB
testcase_15 AC 99 ms
4,348 KB
testcase_16 AC 536 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

std::int64_t NChoose2(const int n) { return std::int64_t{n} * (n - 1) / 2; }

std::int64_t Solve(const int m,
                   const std::vector<int>& l, const std::vector<int>& r) {
  const int n = l.size();
  assert(std::ssize(r) == n);

  std::vector<int> order(n * 2);  // L_i, R_i の昇順にインデックスを並べる
  std::iota(order.begin(), order.end(), 0);
  std::ranges::stable_sort(
      order, {},
      [n, &l, &r](const int i) -> int { return i < n ? l[i] : r[i - n]; });
  assert(order.back() >= n);

  std::int64_t length = std::reduce(l.begin(), l.end(), INT64_C(0));
  if (length > m) return -1;
  std::int64_t ans = std::transform_reduce(
      l.begin(), l.end(), INT64_C(0), std::plus<std::int64_t>(), NChoose2);
  if (length == m) return NChoose2(m) - ans;

  int optional_values = 0;  // 個数が決まっていない値の数
  for (int i = 0; i < n * 2; ++i) {
    if (order[i] < n) {
      const int index = order[i];
      length -= l[index];
      ans -= NChoose2(l[index]);
      ++optional_values;
    } else {
      const int index = order[i] - n;
      length += r[index];
      assert(length <= m);
      ans += NChoose2(r[index]);
      --optional_values;
    }
    if (i + 1 < n * 2) {
      const int next_v = (order[i + 1] < n ?
                          l[order[i + 1]] : r[order[i + 1] - n]);
      // 現在見ている区間に t_0 が存在するか
      if (length + std::int64_t{next_v} * optional_values >= m) {
        assert(optional_values > 0);
        const int t0 = (m - length + optional_values - 1) / optional_values - 1;
        assert((order[i] < n ? l[order[i]] : r[order[i] - n]) <= t0 &&
               t0 < next_v);
        // 個数が t_0 となる値の数
        const int num_of_t0 = (t0 + 1) * optional_values - (m - length);
        assert(0 <= num_of_t0 && num_of_t0 < optional_values);
        ans += NChoose2(t0) * num_of_t0;
        ans += NChoose2(t0 + 1) * (optional_values - num_of_t0);
        return NChoose2(m) - ans;
      }
    }
  }

  assert(length == std::reduce(r.begin(), r.end(), INT64_C(0)) && length < m);
  return -1;
}

int main() {
  constexpr int kMaxT = 200000, kMaxN = 200000, kMaxM = 1000000000;

  int t;
  std::cin >> t;
  assert(1 <= t && t <= kMaxT);

  while (t--) {
    int n, m;
    std::cin >> n >> m;
    assert(1 <= n && n <= kMaxN && 1 <= m && m <= kMaxM);
    std::vector<int> l(n);
    for (int i = 0; i < n; ++i) {
      std::cin >> l[i];
    }
    std::vector<int> r(n);
    for (int i = 0; i < n; ++i) {
      std::cin >> r[i];
      assert(0 <= l[i] && l[i] <= r[i] && r[i] <= kMaxM);
    }
    std::cout << Solve(m, l, r) << '\n';
  }
  return 0;
}
0