結果

問題 No.2114 01 Matching
ユーザー SSRSSSRS
提出日時 2022-10-28 22:36:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,599 bytes
コンパイル時間 3,422 ms
コンパイル使用メモリ 183,188 KB
実行使用メモリ 639,404 KB
最終ジャッジ日時 2023-09-20 05:43:54
合計ジャッジ時間 28,543 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 RE -
testcase_03 AC 143 ms
14,048 KB
testcase_04 AC 180 ms
18,476 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 764 ms
54,508 KB
testcase_12 AC 756 ms
54,584 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 767 ms
54,800 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 MLE -
testcase_25 RE -
testcase_26 AC 795 ms
54,496 KB
testcase_27 AC 796 ms
54,520 KB
testcase_28 AC 387 ms
31,672 KB
testcase_29 AC 832 ms
54,568 KB
testcase_30 RE -
testcase_31 AC 815 ms
54,468 KB
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 AC 276 ms
24,224 KB
testcase_36 AC 780 ms
54,576 KB
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 AC 271 ms
23,620 KB
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 AC 435 ms
54,580 KB
testcase_46 MLE -
testcase_47 AC 770 ms
54,572 KB
testcase_48 AC 787 ms
54,592 KB
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000000;
int main(){
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> B(N);
  for (int i = 0; i < N; i++){
    cin >> B[i];
  }
  vector<int> R(M);
  for (int i = 0; i < M; i++){
    cin >> R[i];
  }
  map<int, pair<vector<int>, vector<int>>> mp;
  for (int i = 0; i < N; i++){
    mp[B[i] % K].first.push_back(i);
  }
  for (int i = 0; i < M; i++){
    mp[R[i] % K].second.push_back(i);
  }
  int edge = 0;
  long long ans = 0;
  for (auto &P : mp){
    int r = P.first;
    vector<int> id1 = P.second.first;
    vector<int> id2 = P.second.second;
    int cnt1 = id1.size();
    int cnt2 = id2.size();
    edge += min(cnt1, cnt2);
    vector<int> B2, R2;
    for (int i = 0; i < cnt1;  i++){
      B2.push_back((B[id1[i]] - r) / K);
    }
    for (int i = 0; i < cnt2;  i++){
      R2.push_back((R[id2[i]] - r) / K);
    }
    sort(B2.begin(), B2.end());
    sort(R2.begin(), R2.end());
    if (cnt1 > cnt2){
      swap(cnt1, cnt2);
      swap(B2, R2);
    }
    assert((long long) cnt1 * cnt2 <= 100000000);
    vector<vector<long long>> dp(cnt1 + 1, vector<long long>(cnt2 + 1, INF));
    dp[0][0] = 0;
    for (int i = 0; i <= cnt1; i++){
      for (int j = 0; j <= cnt2; j++){
        if (j > 0){
          dp[i][j] = min(dp[i][j], dp[i][j - 1]);
          if (i > 0){
            dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + abs(B2[i - 1] - R2[j - 1]));
          }
        }
      }
    }
    ans += dp[cnt1][cnt2];
  }
  if (edge != min(N, M)){
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}
0