当前位置: 首页 > article >正文

【C++算法】分治——归并

排序数组

  • 题目链接

排序数组icon-default.png?t=O83Ahttps://leetcode.cn/problems/sort-an-array/description/

  • 算法原理

  • 代码步骤
class Solution {
    vector<int> tmp;
public:
    vector<int> sortArray(vector<int>& nums) 
    {
        tmp.resize(nums.size());
        merge(nums, 0, nums.size() - 1);
        return nums;
    }

    void merge(vector<int>& nums, int left, int right)
    {
        if(left >= right) return;
        int mid = (right + left) >> 1;
        merge(nums, left, mid);
        merge(nums, mid + 1, right);

        int i = 0, cur1 = left, cur2 = mid + 1;
        while(cur1 <= mid && cur2 <= right)
        {
            tmp[i++] = nums[cur1] <= nums[cur2] ? nums[cur1++] : nums[cur2++];
        }
        while(cur1 <= mid) tmp[i++] = nums[cur1++];
        while(cur2 <= right) tmp[i++] = nums[cur2++];
        for(int i = left; i <= right; i++)
        {
            nums[i] = tmp[i - left];
        }
    }
};

交易逆序对的总数

  • 题目链接

交易逆序对的总数icon-default.png?t=O83Ahttps://leetcode.cn/problems/shu-zu-zhong-de-ni-xu-dui-lcof/description/

  • 算法原理

  • 代码步骤
class Solution {
    vector<int> tmp;
public:
    int reversePairs(vector<int>& record) 
    {
        tmp.resize(record.size());
        return merge(record, 0, record.size() - 1);
    }

    int merge(vector<int>& nums, int left, int right)
    {
        int ret = 0;
        if(left >= right) return 0;
        int mid = (left + right) >> 1;
        ret += merge(nums, left, mid);
        ret += merge(nums, mid + 1, right);

        int i = 0, cur1 = left, cur2 = mid + 1;
        while(cur1 <= mid && cur2 <= right)
        {
            if(nums[cur1] <= nums[cur2]) 
            {
                tmp[i++] = nums[cur1++];
            }
            else
            {
                ret += mid - cur1 + 1;
                tmp[i++] = nums[cur2++];
            }
        }
        while(cur1 <= mid) tmp[i++] = nums[cur1++];
        while(cur2 <= right) tmp[i++] = nums[cur2++];

        for(int i = left; i <= right; i++)
        {
            nums[i] = tmp[i - left];
        }
        return ret;
    }
};

计算右侧小于当前元素的个数

  • 题目链接

计算右侧小于当前元素的个数icon-default.png?t=O83Ahttps://leetcode.cn/problems/count-of-smaller-numbers-after-self/description/

  • 算法原理

  • 代码步骤
class Solution {
    vector<int> ret;
    vector<int> index;
    int tmpNums[100001];
    int tmpIndex[100001];
public:
    vector<int> countSmaller(vector<int>& nums) 
    {
        int n = nums.size();
        ret.resize(n);
        index.resize(n);

        for(int i = 0; i < n; i++)
        {
            index[i] = i;
        }

        mergeSort(nums, 0, n - 1);
        return ret;
    }

    void mergeSort(vector<int>& nums, int left, int right)
    {
        if(left >= right) return;

        int mid = (left + right) >> 1;
        mergeSort(nums, left, mid);
        mergeSort(nums, mid + 1, right);

        int i = 0, cur1 = left, cur2 = mid + 1;
        while(cur1 <= mid && cur2 <= right)
        {
            if(nums[cur1] <= nums[cur2])
            {
                tmpNums[i] = nums[cur2];
                tmpIndex[i++] = index[cur2++];
            }
            else
            {
                ret[index[cur1]] += right - cur2 + 1;
                tmpNums[i] = nums[cur1];
                tmpIndex[i++] = index[cur1++];
            }
        }

        while(cur1 <= mid) 
        {
            tmpNums[i] = nums[cur1];
            tmpIndex[i++] = index[cur1++];
        }
        while(cur2 <= right)
        {
            tmpNums[i] = nums[cur2];
            tmpIndex[i++] = index[cur2++];
        }

        for(int i = left; i <= right; i++)
        {
            nums[i] = tmpNums[i - left];
            index[i] = tmpIndex[i - left];
        }
    }
};

翻转对

  • 题目链接

翻转对icon-default.png?t=O83Ahttps://leetcode.cn/problems/reverse-pairs/description/

  • 算法原理

  • 代码步骤
class Solution {
    vector<int> tmp;
public:
    int reversePairs(vector<int>& nums) 
    {
        int n = nums.size();
        tmp.resize(n);

        return mergeSort(nums, 0, n - 1);
    }

    int mergeSort(vector<int>& nums, int left, int right)
    {
        if(left >= right) return 0;
        int ret = 0;
        int mid = (left + right) >> 1;
        ret += mergeSort(nums, left, mid);
        ret += mergeSort(nums, mid + 1, right);

        int cur1 = left, cur2 = mid + 1, i = 0;
        while(cur1 <= mid && cur2 <= right)
        {
            while(cur2 <= right && nums[cur1] / 2.0 <= nums[cur2])
            {
                cur2++;
            }
            ret += right - cur2 + 1;
            cur1++;
        }

        cur1 = left, cur2 = mid + 1;
        while(cur1 <= mid && cur2 <= right)
        {
            if(nums[cur1] >= nums[cur2]) 
            {
                tmp[i++] = nums[cur1++];
            }
            else
            {
                tmp[i++] = nums[cur2++];
            }
        }
        while(cur1 <= mid)
        {
            tmp[i++] = nums[cur1++];
        }
        while(cur2 <= right)
        {
            tmp[i++] = nums[cur2++];
        }
        
        for(int i = left; i <= right; i++)
        {
            nums[i] = tmp[i - left];
        }
        return ret;
    }
};

http://www.kler.cn/a/303685.html

相关文章:

  • java基础入门学习09-迭代器
  • VTK知识学习(8)-坐标系统
  • Go八股(Ⅴ)map
  • STM32 51单片机设计半导体制冷片温控设计
  • 计算机网络 (1)互联网的组成
  • 【AutoGen 】简介
  • Golang path/filepath包详解:高效路径操作与实战案例
  • 《论面向方面的编程技术及其应用》写作框架,软考高级系统架构设计师
  • 在OpenEuler(欧拉)系统上用kubeadm部署(k8s)Kubernetes集群
  • 【运维监控】系列文章汇总索引
  • linux_L1_linux重启服务器
  • MySQL字符集的转换
  • Integer 缓存
  • Three.js 实战【4】—— 3D地图渲染
  • 【Linux 从基础到进阶】Docker 容器技术基础与应用
  • MySQL事务【后端 13】
  • triton之ttir学习
  • 在C++中,如何避免出现Bug?
  • 第二百三十一节 JPA教程 - JPA Transient示例、 JPA ID注释示例
  • 【ArcGIS Pro】扩展模块 Nuget 使用
  • 【TabBar嵌套Navigation案例-cell重用 Objective-C语言】
  • 【git】.gitignore文件:版本控制的守护者
  • CICD 持续集成与持续交付
  • 论文速读|形机器人的高速和抗冲击远程操作
  • 【LabVIEW学习篇 - 23】:简单状态机
  • 【Android安全】Ubuntu 16.04安装GDB和GEF