# 火柴排队

### 火柴排队

[提交] [状态] [讨论版] [命题人:外部导入]

4
2 3 1 4
3 2 1 4

4
1 3 4 2
1 7 2 4

1

2

4

1 3 4 2

1 7 2 4

1, 2, 3, 4

c[1] = 1;

c[2] = 4;

c[3] = 2;

c[4] = 3;

# include <iostream>
# include <cstring>
# include <cstdio>
# include <algorithm>

using namespace std;
const int maxn = 100005;
long long int k;
const int mod = 99999997;
struct node
{
int id;
int x;
}s_a[maxn], s_b[maxn];
void Merge(int arr[], int start, int mid, int end, int temp[])
{
int first = start;
int second = mid + 1;
int length = 0;
while(first <= mid && second <= end)
{
if(arr[first] <= arr[second])
{
temp[length++] = arr[first++];
}
else
{   k += mid - first + 1;
k%= mod;
temp[length++] = arr[second++];
}
}
while(first <= mid)
{
temp[length++] = arr[first++];
}
while(second <= end)
{
temp[length++] = arr[second++];
}
for(int i = 0; i < length; i++)
{
arr[start + i] = temp[i];
}
}

void Merge_Sort(int arr[], int start, int end, int temp[])
{
if(start >= end)
{
return ;
}
int mid = (start + end) / 2;
Merge_Sort(arr, start, mid, temp);
Merge_Sort(arr, mid + 1, end, temp);
Merge(arr, start, mid, end, temp);
}

bool cmp(struct node a, struct node b)
{
return a.x < b.x;
}
int main()
{
int n;
while(scanf("%d",&n) !=EOF)
{
k = 0;
int temp[maxn];
for(int i = 0; i < n; i++)
{
scanf("%d", &s_a[i].x);
s_a[i].id = i;
}
for(int i = 0; i < n; i++)
{
scanf("%d", &s_b[i].x);
s_b[i].id = i;
}
sort(s_a, s_a + n, cmp);
sort(s_b, s_b + n, cmp);
int arr[maxn];
for(int i = 0; i < n; i++)
{
arr[s_a[i].id] = s_b[i].id;
}
Merge_Sort(arr, 0, n - 1, temp);
cout << k << endl;
}
return 0;
}