結果

問題 No.2114 01 Matching
コンテスト
ユーザー Caiiiiiiii
提出日時 2026-09-25 17:27:11
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,265 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,670 ms
コンパイル使用メモリ 227,756 KB
実行使用メモリ 33,280 KB
最終ジャッジ日時 2026-09-25 17:27:35
合計ジャッジ時間 16,782 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18 WA * 21 RE * 12
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using LL = long long;

const int N = 2e5 + 7;

LL ans;
int n, m, k, cnt, ok;
std::vector<std::pair<int, int>> a[N];
std::vector<int> b[N * 2];
std::map<int, int> mp;

void solve(const std::vector<std::pair<int, int>> &a) {
  int now = N, lo = N, hi = N;
  for(auto &&[x, y]: a) 
    if(y) {
      b[++now].push_back(x);
      hi = std::max(hi, now);
    }
    else {
      b[now--].push_back(x);
      lo = std::min(lo, now);
    }
  if(now < N)
    ok = 0;
  for(int i = lo; i <= hi; ++i) {
    LL sum = 0;
    for(int j = 0; j < b[i].size(); ++j)
      sum += (j & 1 ? b[i][j] : -b[i][j]);
    if(b[i].size() & 1)
      ans += std::min(sum + b[i].back(), -(sum + b[i][0]));
    else
      ans += sum;
    b[i].resize(0);
  }
}

int main() {

  scanf("%d%d%d", &n, &m, &k);
  for(int i = 1, x; i <= n; ++i) {
    scanf("%d", &x);
    if(!mp.count(x % k))
      mp[x % k] = ++cnt;
    a[mp[x % k]].push_back({x / k, n >= m});
  }
  for(int i = 1, x; i <= m; ++i) {
    scanf("%d", &x);
    if(!mp.count(x % k))
      mp[x % k] = ++cnt;
    a[mp[x % k]].push_back({x / k, n < m});
  }

  ok = 1;
  for(auto &&[_, c]: mp) {
    std::sort(a[c].begin(), a[c].end());
    solve(a[c]);
  }

  printf("%lld\n", ok ? ans : -1LL);
  
  return 0;
}
0