分治法——求逆序数**
Posted zengweihao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分治法——求逆序数**相关的知识,希望对你有一定的参考价值。
// test.cpp: 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<iostream> #include <type_traits> using namespace std; int sum; int *b; void merge_sort(int a[], int low, int high); void merge(int a[], int low, int mid, int high); void merge_sort(int a[],int low,int high) { int mid; if (low < high) { mid = (low + high) / 2; merge_sort(a, low, mid); merge_sort(a, mid + 1, high); merge(a, low, mid, high); }
} void merge(int a[], int low, int mid, int high) { int i = low; int j = mid + 1; int k = 0; while (i <= mid && j <= high) { if (a[i] <= a[j]) b[k++] = a[i++]; else { b[k++] = a[j++]; sum += (mid - i + 1);//此处重难点 画图思考 } } while (i <= mid) { b[k++] = a[i++]; } while (j <= high) { b[k++] = a[j++]; } for (k = 0, i = low; i <= high; i++, k++) { a[i] = b[k]; } } int main() { int n, i=0, temp; int a[10000]; cin >> n; sum = 0; b = new int[n]; while(cin >> temp && cin.get() != ‘ ‘)a[i++] = temp; merge_sort(a, 0, n - 1); delete[] b; cout << sum << endl; return 0; }
以上是关于分治法——求逆序数**的主要内容,如果未能解决你的问题,请参考以下文章