【YES】SOJ-2906、2431(归并排序求逆序数)

/******************************************************************************************************
 ** Copyright (C) 2011.06.12(8:000)-2013.07.01
 ** Author: famousDT <[email protected]>
 ** Edit date: 2011-06-12
******************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>//abs,atof(string to float),atoi,atol,atoll
#include <math.h>//atan,acos,asin,atan2(a,b)(a/b atan),ceil,floor,cos,exp(x)(e^x),fabs,log(for E),log10
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <string.h>//memcpy(to,from,count
#include <ctype.h>//character process:isalpha,isdigit,islower,tolower,isblank,iscntrl,isprll
#include <algorithm>
using namespace std;

typedef long long ll;

#define PI acos(-1)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MALLOC(n, type) ((type *)malloc((n) * sizeof(type)))
#define FABS(a) ((a) >= 0 ? (a) : (-(a)))

/* 归并排序求逆序数 */

ll d[1000005];
ll ans = 0;

void merge(ll array[], ll p, ll q, ll r)
{
    ll i, k;
    ll begin1, end1, begin2, end2;
    ll * temp = (ll *)malloc((r - p + 1) * sizeof(ll));
    begin1 = p;
    end1 = q;
    begin2 = q + 1;
    end2 = r;
    k = 0;
    while ((begin1 <= end1) && (begin2 <= end2)) {
        if (array[begin1] < array[begin2]) {
            temp[k] = array[begin1];
            begin1++;
        } else {
            ans += q - begin1 + 1;
            temp[k] = array[begin2];
            begin2++;
        }
        k++;
    }
    while (begin1 <= end1) {
        temp[k++] = array[begin1++];
    }
    while (begin2 <= end2) {
        temp[k++] = array[begin2++];
    }
    for (i = 0; i <= (r - p); i++)
        array[p+i] = temp[i];
    free(temp);
} 

void merge_sort(ll array[], ll first, ll last)
{
    ll mid = 0;
    if (first < last) {
        mid = (first + last) / 2;
        merge_sort(array, first, mid);
        merge_sort(array, mid + 1, last);
        merge(array, first, mid, last);
    }
}

int main()
{
    ll cases;
    ll i;
    ll n;
    while (scanf("%lld", &n) == 1 && n) {
        ans = 0;
        for (i = 0; i < n; ++i)
            scanf("%lld", &d[i]);
        merge_sort(d, 0, n - 1);
        printf("%lld\n", ans);
    }
    return 0;
}

自己写了个在POJ-2299上提交通过

int d[500005];
ll ans;
void merge(int d[], int low, int mid, int high)
{
	int x = low, y = mid + 1;
	int index = 0, i;
	int * t = new int[high - low + 5];
	while (x <= mid && y <= high) {
		if (d[x] <= d[y]) {
			t[index++] = d[x];
			++x;
		} else {
			t[index++] = d[y];
			++y;
			ans += mid - x + 1;
		}
	}
	while (x <= mid) {
		t[index++] = d[x];
		++x;
	}
	while (y <= high) {
		t[index++] = d[y];
		++y;
	}
	for (i = 0; i < index; ++i) {
		d[low + i] = t[i];
	}
	delete t;
}
void merge_sort(int d[], int low, int high)
{
	if (low < high) {
		int mid = (low + high) / 2;
		merge_sort(d, low, mid);
		merge_sort(d, mid + 1, high);
		merge(d, low, mid, high);
	}
}
int main()
{
	int n;
	while (scanf("%d", &n) == 1 && n) {
		int i;
		ans = 0;
		for (i = 0; i < n; ++i) {
			scanf("%d", &d[i]);
		}
		merge_sort(d, 0, n - 1);
		printf("%lld\n", ans);
	}
    return 0;
}


 

你可能感兴趣的:(Date,String,delete,float,交通,merge)