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

ACMSGURU 180 - Inversions

作者头像
Reck Zhang
发布于 2021-08-11 02:13:36
发布于 2021-08-11 02:13:36
27300
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

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
代码运行次数:0
运行
AI代码解释
复制
#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 删除。

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
ACMSGURU 114 - Telecasting station
Every city in Berland is situated on Ox axis. The government of the country decided to build new telecasting station. After many experiments Berland scientists came to a conclusion that in any city citizens displeasure is equal to product of citizens amount in it by distance between city and TV-station. Find such point on Ox axis for station so that sum of displeasures of all cities is minimal.
Reck Zhang
2021/09/01
5490
ACMSGURU 275 - To xor or not to xor
The sequence of non-negative integers A1, A2, …, AN is given. You are to find some subsequence Ai1, Ai2, …, Aik (1 <= i1 < i2 < … < ik <= N) such, that Ai1 XOR Ai2 XOR … XOR Aik has a maximum value.
Reck Zhang
2021/08/11
3640
C++版 - 剑指Offer 面试题36:数组中的逆序对及其变形(Leetcode 315. Count of Smaller Numbers After Self)题解
题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。
Enjoy233
2019/03/05
1.4K0
C++版 - 剑指Offer 面试题36:数组中的逆序对及其变形(Leetcode 315. Count of Smaller Numbers After Self)题解
ACMSGURU 507 - Treediff
Andrew has just made a breakthrough in complexity theory: he thinks that he can prove P=NP if he can get a data structure which allows to perform the following operation quickly. Naturally, you should help him complete his brilliant research. Consider a rooted tree with integers written in the leaves. For each internal (non-leaf) node v of the tree you must compute the minimum absolute difference between all pairs of numbers written in the leaves of the subtree rooted at v.
Reck Zhang
2021/08/11
4170
ACMSGURU 507 - Treediff
ACMSGURU 154 - Factorial
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 12…*N. For example, 5! = 120, 120 contains one zero on the trail.
Reck Zhang
2021/08/11
2900
ACMSGURU 101 - Domino
Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men, stones, or even cards. The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice…
Reck Zhang
2021/08/11
4940
ACMSGURU 104 - Little shop of flowers
You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The vases are glued onto the shelf and are numbered consecutively 1 through V, where V is the number of vases, from left to right so that the vase 1 is the leftmost, and the vase V is the rightmost vase. The bunches are moveable and are uniquely identified by integers between 1 and F. These id-numbers have a significance: They determine the required order of appearance of the flower bunches in the row of vases so that the bunch i must be in a vase to the left of the vase containing bunch j whenever i < j. Suppose, for example, you have bunch of azaleas (id-number=1), a bunch of begonias (id-number=2) and a bunch of carnations (id-number=3). Now, all the bunches must be put into the vases keeping their id-numbers in order. The bunch of azaleas must be in a vase to the left of begonias, and the bunch of begonias must be in a vase to the left of carnations. If there are more vases than bunches of flowers then the excess will be left empty. A vase can hold only one bunch of flowers.
Reck Zhang
2021/08/11
4540
ACMSGURU 398 - Friends of Friends
Social networks are very popular now. They use different types of relationships to organize individual users in a network. In this problem friendship is used as a method to connect users. For each user you are given the list of his friends. Consider friendship as a symmetric relation, so if user a is a friend of user b then b is a friend of a.
Reck Zhang
2021/08/11
2310
ACMSGURU 231 - Prime Sum
Find all pairs of prime numbers (A, B) such that A<=B and their sum is also a prime number and does not exceed N.
Reck Zhang
2021/08/11
3280
ACMSGURU 130 - Circle
On a circle border there are 2k different points A1, A2, …, A2k, located contiguously. These points connect k chords so that each of points A1, A2, …, A2k is the end point of one chord. Chords divide the circle into parts. You have to find N - the number of different ways to connect the points so that the circle is broken into minimal possible amount of parts P.
Reck Zhang
2021/08/11
2640
数据结构之快速排序
上次讲了基于分治法的归并排序,可是归并排序有许多缺点,比如它需要占用额外的内存来存储所需排序的数组,并且整个排序最重要的就是用来合并数组的函数。我写了几次发现,这个合并数组的函数写起来感觉有点麻烦啊!
灯珑LoGin
2022/10/31
4000
数据结构之快速排序
ACMSGURU 134 - Centroid
You are given an undirected connected graph, with N vertices and N-1 edges (a tree). You must find the centroid(s) of the tree. In order to define the centroid, some integer value will be assosciated to every vertex. Let’s consider the vertex k. If we remove the vertex k from the tree (along with its adjacent edges), the remaining graph will have only N-1 vertices and may be composed of more than one connected components. Each of these components is (obviously) a tree. The value associated to vertex k is the largest number of vertices contained by some connected component in the remaining graph, after the removal of vertex k. All the vertices for which the associated value is minimum are considered centroids.
Reck Zhang
2021/08/11
2680
基础知识_算法笔记
1342. Number of Steps to Reduce a Number to Zero
yifei_
2022/11/14
1.6K0
基础知识_算法笔记
ACMSGURU 113 - Nearly prime numbers
Nearly prime number is an integer positive number for which it is possible to find such primes P1 and P2 that given number is equal to P1*P2. There is given a sequence on N integer positive numbers, you are to write a program that prints “Yes” if given number is nearly prime and “No” otherwise.
Reck Zhang
2021/08/11
3540
ACMSGURU 499 - Greatest Greatest Common Divisor
Andrew has just made a breakthrough in sociology: he realized how to predict whether two persons will be good friends or not. It turns out that each person has an inner friendship number (a positive integer). And the quality of friendship between two persons is equal to the greatest common divisor of their friendship number. That means there are prime people (with a prime friendship number) who just can’t find a good friend, andWait, this is irrelevant to this problem. You are given a list of friendship numbers for several people. Find the highest possible quality of friendship among all pairs of given people. Input The first line of the input file contains an integer n (2 <= n <= 10000) — the number of people to process. The next n lines contain one integer each, between 1 and 1000000(inclusive), the friendship numbers of the given people. All given friendship numbers are distinct. Output Output one integer — the highest possible quality of friendship. In other words, output the greatest greatest common divisor among all pairs of given friendship numbers.
Reck Zhang
2021/08/11
4060
ACMSGURU 133 - Border
Along the border between states A and B there are N defence outposts. For every outpost k, the interval [Ak,Bk] which is guarded by it is known. Because of financial reasons, the president of country A decided that some of the outposts should be abandoned. In fact, all the redundant outposts will be abandoned. An outpost i is redundant if there exists some outpost j such that Aj<Ai and Bi<Bj. Your task is to find the number of redundant outposts.
Reck Zhang
2021/08/11
3920
ACMSGURU 117 - Counting
Find amount of numbers for given sequence of integer numbers such that after raising them to the M-th power they will be divided by K.
Reck Zhang
2021/08/11
2760
ACMSGURU 118 - Digital Root
Let f(n) be a sum of digits for positive integer n. If f(n) is one-digit number then it is a digital root for n and otherwise digital root of n is equal to digital root of f(n). For example, digital root of 987 is 6. Your task is to find digital root for expression A1*A2*...*AN + A1*A2*...*AN-1 + ... + A1*A2 + A1.
Reck Zhang
2021/08/11
3080
ACMSGURU 123 - The sum
The Fibonacci sequence of numbers is known: F1 = 1; F2 = 1; Fn+1 = Fn + Fn-1, for n>1. You have to find S - the sum of the first K Fibonacci numbers.
Reck Zhang
2021/08/11
4030
ACMSGURU 127 - Telephone directory
CIA has decided to create a special telephone directory for its agents. The first 2 pages of the directory contain the name of the directory and instructions for agents, telephone number records begin on the third page. Each record takes exactly one line and consists of 2 parts: the phone number and the location of the phone. The phone number is 4 digits long. Phone numbers cannot start with digits 0 and 8. Each page of the telephone directory can contain not more then K lines. Phone numbers should be sorted in increasing order. For the first phone number with a new first digit, the corresponding record should be on a new page of the phone directory. You are to write a program, that calculates the minimal number P pages in the directory. For this purpose, CIA gives you the list of numbers containing N records, but since the information is confidential, without the phones locations.
Reck Zhang
2021/08/11
3320
相关推荐
ACMSGURU 114 - Telecasting station
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验