結果

問題 No.3754 Mischievous Resident (Hard)
コンテスト
ユーザー テナガザル
提出日時 2026-10-02 23:34:15
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,813 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,057 ms
コンパイル使用メモリ 180,352 KB
実行使用メモリ 9,924 KB
最終ジャッジ日時 2026-10-02 23:34:28
合計ジャッジ時間 12,456 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32 WA * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void solve()
{
  const int inf = 1e9;
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; ++i) cin >> a[i];
  for (int i = 0; i < m; ++i) cin >> b[i];
  vector<pair<int, int>> ab(m);
  for (int i = 0; i < m; ++i) ab[i] = {a[i], b[i]};
  sort(ab.begin(), ab.end());
  for (int i = 1; i < m; ++i)
  {
    if (ab[i - 1].second > ab[i].second || (ab[i].first != ab[i - 1].first && ab[i].second == ab[i - 1].second) || (ab[i - 1] == ab[i] && ab[i - 1].first != ab[i - 1].second))
    {
      cout << -1 << endl;
      return;
    }
  }
  auto cal = [](long long l, long long s, long long g, long long r) -> int
  {
    if (s == g) return 0;
    int ret = 0;
    while (s < g)
    {
      ++ret;
      long long ns = min((s + r) / 2, g);
      s = ns;
    }
    while (s > g)
    {
      ++ret;
      long long ns = max((s + l + 1) / 2, g);
      s = ns;
    }
    return ret;
  };
  auto cal2 = [](long long s1, long long g1, long long g2, long long s2) -> int
  {
    long long d1 = g2 - g1, d2 = s2 - s1;
    int ret = 0;
    while (d1 < d2)
    {
      ++ret;
      d1 *= 2;
    }
    return max(2, ret);
  };
  int ans = 0;
  if (ab[0].first > ab[0].second) ++ans;
  if (ab[m - 1].first < ab[m - 1].second) ++ans;
  for (int i = 0; i + 1 < m; ++i)
  {
    if (ab[i].first < ab[i].second)
    {
      if (ab[i + 1].first > ab[i + 1].second) ans += cal2(ab[i].first, ab[i].second, ab[i + 1].second, ab[i + 1].first);
      else ans += cal(-inf, ab[i].first, ab[i].second, ab[i + 1].second);
    }
    else if (ab[i + 1].first > ab[i + 1].second) ans += cal(ab[i].second, ab[i + 1].first, ab[i + 1].second, inf + inf);
  }
  cout << ans << endl;
}

int main()
{
  int t;
  cin >> t;
  while (t--) solve();
}
0