HDU 1394 minimum inversion number (tree array)

Source: Internet
Author: User

Question Link: Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1394


Analysis:

First, this is the problem of solving the reverse order number. First, we need to know what the reverse order number is?

In an arrangement, if the front and back positions of a pair of numbers are in the opposite order of size, that is, the front number is greater than the back number, they are called a reverse order. The total number of reverse orders in an arrangement is called the number of reverse orders in this arrangement.


The reverse logarithm naturally comes to mind with a tree array, which is convenient and fast.


According to the meaning of the question, the arrangement is to move the first element to the final arrangement, so we can start from here,

For the first element, if it is smaller than it, it will certainly form a reverse order. In this way, when the first element is moved to the end of the current reverse order, its reverse logarithm reduces the value of this element and then minus one. Because the value of this question is continuous, it can be directly reduced, the number greater than the value of this element and it will form a reverse order. In this way, after the previous value is reduced, we need to add the total number of elements minus the value of this element. In this way, the obtained value is the reverse logarithm of the new arrangement. For example, if we want to move a [0] to the end, the total number of elements is N, and the current reverse logarithm is sum, then when we move a [0] to the end, sum + = n-A [0]-1-A [0].


With this method, we can calculate the smallest reverse logarithm of all sorts in O (n) time. The total time complexity is O (nlogn ).


Code:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <vector>using namespace std;const int MAXN=5005;#define Lowbit(x) ((x)&(-(x)))int a[MAXN], c[MAXN];int N;void ADD(int p, int val){    while(p>0){        c[p]+=val;        p-=Lowbit(p);    }}int getsum(int p){    int sum = 0;    p++;    while(p<=N){        sum += c[p];        p+=Lowbit(p);    }    return sum;}int main(){    while(~scanf("%d", &N)){        int i,x;        int sum = 0;        memset(c, 0, sizeof(c));        for(i=0; i<N; ++i){            scanf("%d", &a[i]);            sum  += getsum(a[i]+1);            ADD(a[i]+1, 1);            //printf("%d\n", sum);        }        int ans = sum;        for(i=0; i<N; ++i){            sum+=(N-1-2*a[i]);            ans = min(ans, sum);        }        printf("%d\n", ans);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.