-
Notifications
You must be signed in to change notification settings - Fork 0
/
YODANESS.cpp
79 lines (75 loc) · 1.18 KB
/
YODANESS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "bits/stdc++.h"
#define ll long long int
using namespace std;
ll inversion;
void merge(int arr[],int *L,int *R,int left,int right)
{
int i=0,j=0,k=0;
while(i<left and j<right)
{
if (L[i]>R[j])
{
arr[k++]=R[j++];
inversion+=left-i;
}
else
{
arr[k++]=L[i++];
}
}
while(i<left)arr[k++]=L[i++];
while(j<right)arr[k++]=R[j++];
}
void mergesort(int arr[],int length)
{
if(length<2) return;
int mid=length/2;
int *L=new int[mid];
int *R=new int[length-mid];
for (int i = 0; i < mid; ++i)
{
L[i]=arr[i];
}
for (int i = mid; i < length; ++i)
{
R[i-mid]=arr[i];
}
mergesort(L,mid);
mergesort(R,length-mid);
merge(arr,L,R,mid,length-mid);
delete[] L;
delete[] R;
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
int t;
cin>>t;
while(t--)
{
int length;
cin>>length;
string ss;
map<string,int> map;
std::vector<string> v;
for (int i = 0; i < length; ++i)
{
cin>>ss;
v.push_back(ss);
}
for (int i = 0; i < length; ++i)
{
cin>>ss;
map[ss]=i;
}
int arr[30005];
for (int i = 0; i < v.size(); ++i)
{
arr[i]=map[v[i]];
}
mergesort(arr,length);
cout<<inversion<<endl;
inversion=0;
}
return 0;
}