結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー HaarHaar
提出日時 2020-07-17 21:46:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 1,340 bytes
コンパイル時間 2,632 ms
コンパイル使用メモリ 208,692 KB
実行使用メモリ 9,720 KB
最終ジャッジ日時 2023-08-20 00:27:19
合計ジャッジ時間 6,082 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 112 ms
8,876 KB
testcase_04 AC 132 ms
9,536 KB
testcase_05 AC 112 ms
8,652 KB
testcase_06 AC 102 ms
8,272 KB
testcase_07 AC 131 ms
9,652 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 53 ms
6,772 KB
testcase_10 AC 95 ms
9,720 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 135 ms
9,652 KB
testcase_13 AC 135 ms
9,584 KB
testcase_14 AC 134 ms
9,588 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 10 ms
4,380 KB
testcase_24 AC 43 ms
5,452 KB
testcase_25 AC 97 ms
8,084 KB
testcase_26 AC 21 ms
4,504 KB
testcase_27 AC 54 ms
5,948 KB
testcase_28 AC 76 ms
6,992 KB
testcase_29 AC 105 ms
8,300 KB
testcase_30 AC 128 ms
9,460 KB
testcase_31 AC 30 ms
5,068 KB
testcase_32 AC 19 ms
4,376 KB
testcase_33 AC 105 ms
8,792 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,380 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