結果

問題 No.2501 Maximum Inversion Number
ユーザー 👑 emthrmemthrm
提出日時 2023-07-13 16:07:48
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,669 bytes
コンパイル時間 1,075 ms
コンパイル使用メモリ 108,184 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-20 00:39:13
合計ジャッジ時間 3,817 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 RE -
testcase_02 AC 177 ms
6,940 KB
testcase_03 RE -
testcase_04 AC 85 ms
6,940 KB
testcase_05 AC 87 ms
6,940 KB
testcase_06 AC 91 ms
6,944 KB
testcase_07 AC 98 ms
6,944 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 93 ms
6,940 KB
testcase_11 AC 72 ms
6,940 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 115 ms
6,940 KB
testcase_15 RE -
testcase_16 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

// <WA> #890216 ベース
// 二分探索の上界の初期値が間違っている。
std::int64_t Solve(
    int m, const std::vector<int>& l, const std::vector<int>& r) {
  const int n = l.size();
  assert(std::ssize(r) == n);

  if (std::reduce(l.begin(), l.end(), INT64_C(0)) > m) return -1;

  int t0 = m;  // 本来は m + 1
  for (int lbound = 0; lbound + 1 < t0;) {
    const int t = std::midpoint(lbound, t0);
    std::int64_t length = 0;
    for (int i = 0; i < n; ++i) {
      length += std::clamp(t, l[i], r[i]);
    }
    (length >= m ? t0 : lbound) = t;
  }

  std::int64_t ans = NChoose2(m);
  int num_of_t0 = 0;
  for (int i = 0; i < n; ++i) {
    const int c_i = std::clamp(t0, l[i], r[i]);
    m -= c_i;
    ans -= NChoose2(c_i);
    if (l[i] < t0 && t0 <= r[i]) ++num_of_t0;
  }
  assert(m <= 0 && -m <= num_of_t0);
  ans -= (NChoose2(t0 - 1) - NChoose2(t0)) * -m;
  return ans;
}

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