1019 逆序数 (归并排序 线段树)

1019 逆序数
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
收藏
关注
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。
如2 4 3 1中,2 1,4 3,4 1,3 1是逆序,逆序数是4。给出一个整数序列,求该序列的逆序数。
Input

第1行:N,N为序列的长度(n <= 50000)
第2 - N + 1行:序列中的元素(0 <= A[i] <= 10^9)

Output

输出逆序数

Input示例

4
2
4
3
1

Output示例

4
分治法:归并排序

#include <iostream>
#include <cstdio>

using namespace std;
int ans=0;

void Merge(int a[],int l,int mid,int r,int temp[])
{
    int i=l,j=mid+1,k=0;
    while(i<=mid&&j<=r){
        if(a[i]<=a[j]){
            temp[k++]=a[i++];
        }
        else{
            temp[k++]=a[j++];
            ans+=(mid-i+1);
        }
    }
    while(i<=mid){
        temp[k++]=a[i++];
    }
    while(j<=r){
        temp[k++]=a[j++];
    }
    k=0;
    while(l<=r){
        a[l++]=temp[k++];
    }
}

void merge_sort(int a[],int l,int r,int temp[])
{
    if(l<r){
        int mid=(l+r)/2;
        merge_sort(a,l,mid,temp);
        merge_sort(a,mid+1,r,temp);
        Merge(a,l,mid,r,temp);
    }
}

int main()
{
    int n;
    int a[50005];
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        ans=0;
        int temp[50005];
        merge_sort(a,0,n-1,temp);
        printf("%d\n",ans);
    }
    return 0;
}

线段树离散化
https://blog.csdn.net/qq_34271269/article/details/52007116


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cctype>
#define inf 1e9+7
using namespace std;

const int maxn = 50005;
int arr[maxn];
int brr[maxn];
int hashh[maxn];
int save[maxn];
int sum[maxn*4];
vector<int> q;
int n,m;
void update(int num,int l,int r,int x)
{
    if(l==r) {sum[num]++;return;}
    int mid=(l+r)>>1;
    if(x<=mid) update(num<<1,l,mid,x);
    else if(x>mid) update((num<<1)|1,mid+1,r,x);
    sum[num]++;
}
int query(int num,int l,int r,int left,int right)
{
    if(l==left&&r==right) return sum[num];
    int mid=(l+r)>>1;
    if(right<=mid) return query(num<<1,l,mid,left,right);
    else if(left>mid) return query((num<<1)|1,mid+1,r,left,right);
    else return query(num<<1,l,mid,left,mid)+query((num<<1)|1,mid+1,r,mid+1,right);
}
int getid(int x) {return lower_bound(save+1,save+m+1,x)-save;}
int main()
{
    while(scanf("%d",&n)!=EOF&&n){
    memset(sum,0,sizeof(sum));
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&arr[i]);
        hashh[i]=arr[i];
    }
        sort(hashh+1,hashh+n+1);
        m=1;save[1]=hashh[1];
        for(int i=2;i<=n;i++) if(hashh[i]!=hashh[i-1]) save[++m]=hashh[i];
        int size=m;
        for(int i=1;i<=n;i++) brr[i]=getid(arr[i]);
    long long cnt=0;
    for(int i=1;i<=n;i++)
    {
        update(1,1,size+10,brr[i]);
        cnt+=(long long)query(1,1,size+10,brr[i]+1,size+10);
    }
    cout<<cnt<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值