复习算法:归并排序解决逆序数问题(分治)
发布时间
阅读量:
阅读量

解析:本题通过采用归并排序算法来处理逆序对数量的计算问题,其中元素交换的次数恰好对应于逆序对的总数。
代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
void merge(vector<int>&a, int low, int high, int mid, int& count)
{
vector<int>temp;
int i = low, j = mid + 1;
while (i <= mid and j <= high)
{
if (a[i] > a[j])
{
count++;
全部评论 (0)
还没有任何评论哟~
