結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー HaarHaar
提出日時 2020-07-17 21:46:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,340 bytes
コンパイル時間 2,158 ms
コンパイル使用メモリ 211,308 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-05-07 07:54:55
合計ジャッジ時間 5,246 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 114 ms
9,088 KB
testcase_04 AC 132 ms
9,728 KB
testcase_05 AC 114 ms
8,832 KB
testcase_06 AC 101 ms
8,448 KB
testcase_07 AC 133 ms
9,728 KB
testcase_08 AC 6 ms
6,940 KB
testcase_09 AC 54 ms
7,040 KB
testcase_10 AC 98 ms
9,856 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 136 ms
9,856 KB
testcase_13 AC 132 ms
9,856 KB
testcase_14 AC 137 ms
9,856 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,948 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 10 ms
6,940 KB
testcase_24 AC 45 ms
6,944 KB
testcase_25 AC 99 ms
8,192 KB
testcase_26 AC 21 ms
6,940 KB
testcase_27 AC 55 ms
6,940 KB
testcase_28 AC 75 ms
7,168 KB
testcase_29 AC 106 ms
8,448 KB
testcase_30 AC 128 ms
9,472 KB
testcase_31 AC 30 ms
6,940 KB
testcase_32 AC 18 ms
6,940 KB
testcase_33 AC 104 ms
8,960 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 1 ms
6,940 KB
testcase_38 AC 1 ms
6,944 KB
testcase_39 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

/**
 * @title Inversion number
 * @docs inversion_number.md
 */
namespace inversion_number{
  template <typename T, typename Compare>
  int64_t rec(std::vector<T> &a, const Compare &compare){
    int n = a.size();
    if(n <= 1) return 0;
    
    int64_t ret = 0;
    
    std::vector<T> b(a.begin(), a.begin() + n/2);
    std::vector<T> c(a.begin() + n/2, a.end());
    
    ret += rec(b, compare);
    ret += rec(c, compare);
    
    int ai = 0, bi = 0, ci = 0;
    
    while(ai < n){
      if(bi < (int)b.size() and (ci == (int)c.size() or not compare(b[bi], c[ci]))){
        a[ai] = b[bi];
        ++bi;
      }else{
        ret += n/2 - bi;
        a[ai] = c[ci];
        ++ci;
      }
      ++ai;
    }
    
    return ret;
  }
  
  template <typename T, typename Compare>
  int64_t solve(std::vector<T> a, const Compare &compare){
    return rec(a, compare);
  }
}



int main(){
  int N;
  while(std::cin >> N){
    std::vector<int> A(N), B(N);
    for(int i = 0; i < N; ++i) std::cin >> A[i], A[i] -= 1;
    for(int i = 0; i < N; ++i) std::cin >> B[i], B[i] -= 1;

    std::map<int, int> m;
    for(int i = 0; i < N; ++i) m[B[i]] = i;
    
    for(int i = 0; i < N; ++i) A[i] = m[A[i]];

    auto ans = inversion_number::solve(A, std::greater<>());

    std::cout << ans << "\n";
  }
  
  return 0;
}
0