The idea of merging and sorting solves the small sum problem and the reverse pair problem

Welcome everyone to pay attention to my WeChat

official account and update the blog synchronously for easy viewing. The small sum problem and the reverse order problem are an extension of the merge sort algorithm. This blog will implement the solution of the small sum problem and reverse order problem.

Small sum problem

  • In an array, the numbers on the left side of each number that are smaller than the current number are added up, called the small sum of this array, find the small sum of an array
  • Time complexity O(N*logN), space complexity O(N)
/*
    @Author: lwl2020
	@Date: 2020-5-20
	@Description: 小和问题的实现
*/

#include <iostream>
#include <vector>
#include <ctime>
using namespace std;

class CSmallSum {
    
    
public:
	int smallSum(vector<int>& arr) {
    
    
		if (arr.size() < 2) {
    
    
			return 0;
		}
		return mergeSortProcess(arr, 0, arr.size() - 1);
	}

private:
	int mergeSortProcess(vector<int>& arr, int left, int right) {
    
    
		if (left == right) {
    
    
			return 0;
		}
		int mid = left + ((right - left) >> 1);
		return mergeSortProcess(arr, left, mid)
			+ mergeSortProcess(arr, mid + 1, right)
			+ merge(arr, left, mid, right);
	}

	int merge(vector<int>& arr, int left, int mid, int right) {
    
    
		int tmpL = left;
		int tmpR = mid + 1;
		int res = 0;
		vector<int> help;
		while (tmpL <= mid && tmpR <= right) {
    
    
			// 生成小和的关键一步
			res += arr.at(tmpL) < arr.at(tmpR) ? arr.at(tmpL) * (right - tmpR + 1) : 0;
			help.push_back(arr.at(tmpL) < arr.at(tmpR) ? arr.at(tmpL++) : arr.at(tmpR++));
		}
		while (tmpL <= mid) {
    
    
			help.push_back(arr.at(tmpL++));
		}
		while (tmpR <= right) {
    
    
			help.push_back(arr.at(tmpR++));
		}
		for (int i = 0; i < help.size(); i++) {
    
    
			arr.at(left + i) = help.at(i);
		}
		return res;
	}
};

int main() {
    
    
	vector<int> arr{
    
     1, 3, 4, 2, 5 };
	int res = CSmallSum().smallSum(arr);
	cout << res << endl;

	system("pause");
	return 0;
}

Reverse pair problem

  • In an array, if the number on the left is greater than the number on the right, these two numbers form a reverse pair. Please print all reverse pairs
  • The solution to the problem of reverse order is very similar to the small sum problem, and it is also an extended application of merge sort
/*
    @Author: lwl2020
	@Date: 2020-5-20
	@Description: 逆序对问题的实现
*/

#include <iostream>
#include <vector>
using namespace std;

class CInversionPair {
    
    
public:
	void inversionPair(vector<int>& arr) {
    
    
		if (arr.size() < 2) {
    
    
			return;
		}
		mergeSortProcess(arr, 0, arr.size() - 1);
	}

private:
	void mergeSortProcess(vector<int>& arr, int left, int right) {
    
    
		if (left == right) {
    
    
			return;
		}
		int mid = left + ((right - left) >> 1);
		mergeSortProcess(arr, left, mid);
		mergeSortProcess(arr, mid + 1, right);
		merge(arr, left, mid, right);
	}

	void merge(vector<int>& arr, int left, int mid, int right) {
    
    
		int tmpL = left;
		int tmpR = mid + 1;
		vector<int> help;
		while (tmpL <= mid && tmpR <= right) {
    
    
			//********** 生成并打印逆序对 **********
			if (arr.at(tmpL) > arr.at(tmpR)) {
    
    
				for (int i = 0; i <= mid - tmpL; i++) {
    
    
					cout << "(" << arr.at(tmpL + i) << ", " << arr.at(tmpR) << ")" << endl;
				}
			}
			//**************************************
			help.push_back(arr.at(tmpL) < arr.at(tmpR) ? arr.at(tmpL++) : arr.at(tmpR++));
		}
		while (tmpL <= mid) {
    
    
			help.push_back(arr.at(tmpL++));
		}
		while (tmpR <= right) {
    
    
			help.push_back(arr.at(tmpR++));
		}
		for (int i = 0; i < help.size(); i++) {
    
    
			arr.at(left + i) = help.at(i);
		}
	}
};

int main() {
    
    
	vector<int> arr{
    
     1, 5, 3, 2, 4 };
	CInversionPair().inversionPair(arr);

	system("pause");
	return 0;
}

If there is any infringement, please contact to delete it. If there is an error, please correct me, thank you

Guess you like

Origin blog.csdn.net/xiao_ma_nong_last/article/details/106231175