#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10000005;
const int mod = 99999997;
struct fire
{
int h;
int No;
}f1[maxn],f2[maxn];
long long c[maxn], p[maxn] ,ans=0; //用于存储排序后的答案
bool cmp(fire f1, fire f2)
{
return f1.h < f2.h;
}
void msort(int start,int end)
{
if (start == end)
{
return;
}
int mid = (start + end) / 2;
msort(start, mid); //对分开的两部分分别进行排序
msort(mid + 1, end);
//排好序后进行合并
int i = start;
int j = mid + 1;
int k = start;
while (i <= mid && j <= end)
{
while (c[i] <= c[j] && i <= mid && j <= end)
{
p[k++] = c[i++];
}
while (c[i] > c[j] && i <= mid && j <= end)
{
p[k++] = c[j++];
//这里就产生了逆序
ans = (ans+mid - i + 1)%mod;
}
}
while (i <= mid)
{
p[k++] = c[i++];
}
while (j <= end)
{
p[k++] = c[j++];
}
for (int i = start; i <= end; i++)
{
c[i] = p[i];
}
}
int main()
{
long long n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> f1[i].h;
f1[i].No = i;
}
for (int i = 1; i <= n; i++)
{
cin >> f2[i].h;
f2[i].No = i;
}
sort(f1 + 1, f1 + n + 1, cmp);
sort(f2 + 1, f2 + n + 1, cmp);
//排序后就用c来存储
for (int i = 1; i <= n; i++)
{
c[f2[i].No] = f1[i].No;
}
//现在对数组C进行归并排序
msort(1, n);
printf("%lld", ans);
return 0;
}
P1966 [NOIP2013 提高组] 火柴排队
最新推荐文章于 2024-08-28 22:04:30 发布