逆序数Sicily 5142. Inversion Number

//
//  main.cpp
//  algorithm
//
//  Created by Calvin Zang on 12-5-5.
//  Copyright (c) 2012年 SYSU. All rights reserved.
//
#include 
#include 
#include 
#include 


typedef int TYPE;
#define N 100002
TYPE A[N];
int binarySearch(TYPE A[], int p, int r, TYPE key)
{
    int mid;
    while(p <= r){
        mid = (p+r)/2;
        if(key < A[mid]) r = mid-1;
        else if(key > A[mid]) p = mid+1;
        else return mid;
    }
    return p;
}
long long cntInversion = 0;
void merge(TYPE A[], int p, int q, int r)
{
    int n1 = q-p+1, n2 = r-q, i, j, k;
    //TYPE L[N/2+2], R[N/2+2];
    TYPE *L = (TYPE*)malloc(sizeof(TYPE)*n1);
    TYPE *R = (TYPE*)malloc(sizeof(TYPE)*n2);
    for(i = 0; i < n1; ++i) L[i] = A[p+i];
    for(i = 0; i < n2; ++i) R[i] = A[q+1+i];
    for(i = 0; i < n2; ++i) 
        cntInversion += (n1-binarySearch(L, 0, n1-1, R[i]));
    i = j = 0; k = p;
    while(i

你可能感兴趣的:(OJ,merge,n2,c)