归并排序 (求逆序数)

数据结构实验之排序五:归并求逆序数

Time Limit: 50 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

z6q6k6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值