前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ACMSGURU 180 - Inversions

ACMSGURU 180 - Inversions

作者头像
Reck Zhang
发布2021-08-11 10:13:36
2580
发布2021-08-11 10:13:36
举报
文章被收录于专栏:Reck Zhang

Inversions

Problem Description

There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j].

Input

The first line of the input contains the number N. The second line contains N numbers A1…AN.

Output

Write amount of such pairs.

Sample test(s)

Input

5 2 3 1 5 4

Output

3

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);

    int n{};
    std::cin >> n;

    std::vector<int> vec(n, 0);
    std::vector<int> tmp(n, 0);

    for(int i = 0; i < n; i++) {
        std::cin >> vec[i];
    }

    long long res{0};

    std::function<void(int, int)> mergeSort;
    mergeSort = [&](int left, int right){
        if(right - left > 1) {
            int mid = (left + (right - left) / 2);
            int p = left;
            int q = mid;
            int index = left;
            mergeSort(left, mid);
            mergeSort(mid, right);
            while(p < mid || q < right) {
                if(q >= right || (p < mid && vec[p] <= vec[q])) {
                    tmp[index++] = vec[p++];
                } else {
                    tmp[index++] = vec[q++];
                    res += mid - p;
                }
            }
            for(index = left; index < right; index++) {
                vec[index] = tmp[index];
            }
        }
    };

    mergeSort(0, n);

    std::cout << res;

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-10-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Inversions
    • Problem Description
      • Input
        • Output
          • Sample test(s)
            • Input
              • Output
                • Solution
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档