結果
| 問題 |
No.1115 二つの数列 / Two Sequences
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-08-17 12:43:38 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 87 ms / 2,000 ms |
| コード長 | 922 bytes |
| コンパイル時間 | 2,374 ms |
| コンパイル使用メモリ | 205,652 KB |
| 最終ジャッジ日時 | 2025-01-30 23:26:38 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 35 |
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
long long inversion_number(vector<int> &A){
int N = A.size();
vector<int> B = A;
sort(B.begin(), B.end());
map<int, int> mp;
for (int i = 0; i < N; i++) mp[B[i]] = i + 1;
for (int i = 0; i < N; i++) A[i] = mp[A[i]];
long long ans = 0;
vector<int> BIT(N + 1, 0);
for (int i = 0; i < N; i++){
ans += i; int j;
j = A[i]; while (j > 0){ ans -= BIT[j]; j -= j & -j; }
j = A[i]; while (j <= N){ BIT[j]++; j += j & -j; }
}
return ans;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n; cin >> n;
vector<int> a(n), b(n);
rep(i,n) cin >> a[i], a[i]--;
rep(i,n) cin >> b[i], b[i]--;
vector<int> bpos(n);
rep(i,n) bpos[b[i]] = i;
rep(i,n) a[i] = bpos[a[i]];
cout << inversion_number(a) << endl;
}