sgu 180 Inversions

180. Inversions
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
input: standard
output: standard



There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j].

Input
The first line of the input contains the number N. The second line contains N numbers A1...AN.

Output
Write amount of such pairs.

Sample test(s)

Input
5
2 3 1 5 4

Output
3

逆序对统计,就当复习下归排吧。。

贴代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)

using namespace std;
int const nMax = 70010;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;

//    std::ios::sync_with_stdio(false);

int a[nMax],c[nMax];
long long ans;
int n;

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

int main(){
    scanf("%d",&n);
    FOR(i,n) scanf("%d",&a[i]);
    ans=0;
    merge_sort(1,n);
    printf("%I64d\n",ans);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值