数据结构实验之排序五:归并求逆序数
Time Limit: 50 ms Memory Limit: 65536 KiB
Problem Description
对于数列a1,a2,a3…中的任意两个数ai,aj (i < j),如果ai > aj,那么我们就说这两个数构成了一个逆序对;在一个数列中逆序对的总数称之为逆序数,如数列 1 6 3 7 2 4 9中,(6,4)是一个逆序对,同样还有(3,2),(7,4),(6,2),(6,3)等等,你的任务是对给定的数列求出数列的逆序数。
Input
输入数据N(N <= 100000)表示数列中元素的个数,随后输入N个正整数,数字间以空格间隔。
Output
输出逆序数。
Sample Input
10
10 9 8 7 6 5 4 3 2 1
Sample Output
45
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll sum;
int a[100010];
int temp[100010];
void merge(int l, int mid, int r)
{
int tail=0;
int i=l, j=mid+1;
while(i<=mid&&j<=r)
{
if(a[i]<=a[j])
temp[tail++]=a[i++];
else
{
temp[tail++]=a[j++];
sum+=(mid-i+1);//求逆序数重点
}
}
while(i<=mid)
{
temp[tail++]=a[i++];
}
while(j<=r)
{
temp[tail++]=a[j++];
}
for(int i=l;i<=r;i++)
a[i]=temp[i-l];
}
void merge_sort(int l, int r)
{
int mid=(l+r)/2;
if(l==r)
return ;
else
{
merge_sort(l, mid);
merge_sort(mid+1, r);
merge(l, mid, r);
}
}
int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d", &a[i]);
sum=0;
merge_sort(0, n-1);
//for(int i=0;i<n;i++)
printf("%lld\n", sum);
// printf("\n");
}
}
HDU6318
Swaps and Inversions
Long long ago, there was an integer sequence a.
Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
You don't want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
What is the minimum amount of money you need to spend?
The definition of inversion in this problem is pair (i,j)(i,j) which 1≤i<j≤n1≤i<j≤n and ai>ajai>aj.
Input
There are multiple test cases, please read till the end of input file.
For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
In the second line, n integers separated by spaces, representing the orginal sequence a.
1≤n,x,y≤1000001≤n,x,y≤100000, numbers in the sequence are in [−109,109][−109,109]. There're 10 test cases.
Output
For every test case, a single integer representing minimum money to pay.
Sample Input
3 233 666
1 2 3
3 1 666
3 2 1
Sample Output
0
3
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll sum;
int a[100010];
int temp[100010];
void merge(int l, int mid, int r)
{
int tail=0;
int i=l, j=mid+1;
while(i<=mid&&j<=r)
{
if(a[i]<=a[j])
temp[tail++]=a[i++];
else
{
temp[tail++]=a[j++];
sum+=(mid-i+1);
}
}
while(i<=mid)
{
temp[tail++]=a[i++];
}
while(j<=r)
{
temp[tail++]=a[j++];
}
for(int i=l;i<=r;i++)
a[i]=temp[i-l];
}
void merge_sort(int l, int r)
{
int mid=(l+r)/2;
if(l==r)
return ;
else
{
merge_sort(l, mid);
merge_sort(mid+1, r);
merge(l, mid, r);
}
}
int main()
{
int n, x, y;
while(scanf("%d%d%d", &n, &x, &y)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d", &a[i]);
sum=0;
merge_sort(0, n-1);
//for(int i=0;i<n;i++)
printf("%lld\n", sum*min(x, y));
// printf("\n");
}
}
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll sum;
int a[100010], b[100010];
int temp[100010];
void merge(int l, int mid, int r)
{
int tail=0;
int i=l, j=mid+1;
while(i<=mid&&j<=r)
{
if(a[i]<=a[j])
temp[tail++]=a[i++];
else
{
temp[tail++]=a[j++];
sum+=(mid-i+1);//求逆序数重点
}
}
while(i<=mid)
{
temp[tail++]=a[i++];
}
while(j<=r)
{
temp[tail++]=a[j++];
}
for(int i=l;i<=r;i++)
a[i]=temp[i-l];
}
void merge_sort(int l, int r)
{
int mid=(l+r)/2;
if(l==r)
return ;
else
{
merge_sort(l, mid);
merge_sort(mid+1, r);
merge(l, mid, r);
}
}
int main()
{
int n;
int aa, bb;
int t;
scanf("%d", &t);
while(t--)
{scanf("%d", &n);
int tail=0;
for(int i=0;i<n;i++)
{
scanf("%d", &aa);
b[aa]=i;
}
for(int i=0;i<n;i++)
{
scanf("%d", &bb);
a[i]=b[bb];
}
sum=0;
merge_sort(0, n-1);
printf("%lld\n", sum);
}
return 0;
}