number of reversed pairs

number of reversed pairs

Given an array of integers of length n, please count the number of reversed pairs in the array.

The reverse pair is defined as follows: For the i-th and j-th elements of the array, if i<j
  And a[i]>a[j], then it is a reversed pair; otherwise it is not.

input format
The first line contains the integer n, indicating the length of the sequence.
The second line contains n integers representing the entire sequence.

output format
Output an integer representing the number of reversed pairs.

data range
1≤n≤100000 The value range of elements in the array [1,1e9]

Input sample:
6
2 3 4 5 6 1
Sample output:
5

Solution

This code implements the merge sort algorithm and counts the number of reversed pairs. A reverse pair means that in an array, if there are two elements a[i] and a[j], and i < j but a[i] > a[j], then these two elements constitute a reverse pair.

Specifically, this code implements a function called mergeSort that takes three parameters: the array to be sorted q, the left edge of the array l, and the right edge of the array r. The function first checks whether the left and right boundaries are equal, and returns 0 directly if they are equal. Otherwise, the function calculates the middle position mid of the array, then recursively calls the mergeSort function to sort the left and right sub-arrays, and adds the number of reversed pairs after sorting. After the sorting is completed, the function merges the left and right sorted sub-arrays into an ordered array, and returns the number of reversed pairs after the merge.

When merging two sorted subarrays, the function uses a temporary array named tmp. The function first clears the tmp array to zero, and then uses two pointers i and j to point to the starting positions of the left and right sub-arrays respectively. The function compares the size of q[i] and q[j], puts the smaller element into the tmp array, and moves the pointer to that element backwards. If q[i] is greater than q[j], it means that q[i] and the following elements are greater than q[j], so the number of reversed pairs can be calculated and added to res. Finally, the function copies the elements of the tmp array back into the original array q.

In the main function, the program reads an integer n and an array a of length n from the standard input, then calls the mergeSort function to sort the array a, and outputs the number of reversed pairs after sorting.

Difficult

For res+= mid - i + 1

This code implements the merge sort algorithm and counts the number of reversed pairs. A reverse pair means that in an array, if there are two elements a[i] and a[j], and i < j but a[i] > a[j], then these two elements constitute a reverse pair.

Specifically, this code implements a function called mergeSort that takes three parameters: the array to be sorted q, the left edge of the array l, and the right edge of the array r. The function first checks whether the left and right boundaries are equal, and returns 0 directly if they are equal. Otherwise, the function calculates the middle position mid of the array, then recursively calls the mergeSort function to sort the left and right sub-arrays, and adds the number of reversed pairs after sorting. After the sorting is completed, the function merges the left and right sorted sub-arrays into an ordered array, and returns the number of reversed pairs after the merge.

When merging two sorted subarrays, the function uses a temporary array named tmp. The function first clears the tmp array to zero, and then uses two pointers i and j to point to the starting positions of the left and right sub-arrays respectively. The function compares the size of q[i] and q[j], puts the smaller element into the tmp array, and moves the pointer to that element backwards. If q[i] is greater than q[j], it means that q[i] and the following elements are greater than q[j], so the number of reversed pairs can be calculated and added to res. Finally, the function copies the elements of the tmp array back into the original array q.

In the main function, the program reads an integer n and an array a of length n from the standard input, then calls the mergeSort function to sort the array a, and outputs the number of reversed pairs after sorting.

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 100010;

int n;
int a[N], tmp[N];

int mergeSort(int q[], int l, int r) {
    if (l >= r) return 0;
    int mid = (l + r) >> 1;
    ll res = mergeSort(q, l, mid) + mergeSort(q, mid + 1, r);
    int k = 0, i = l, j = mid + 1;
    while (i <= mid && j <= r) {
        if (q[i] <= q[j]) tmp[k ++ ] = q[i ++ ];
        else {
            res += mid - i + 1;
            tmp[k ++ ] = q[j ++ ];
        } 
    }
    while (i <= mid) tmp[k ++ ] = q[i ++ ];
    while (j <= r) tmp[k ++ ] = q[j ++ ];

    for (i = l, j = 0; i <= r; i ++ , j ++ ) q[i] = tmp[j];

    return res;
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i ++ ) cin >> a[i];

    cout << mergeSort(a, 0, n - 1);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值