Minimum Inversion Number

本文深入探讨了树状数组在计算逆序数方面的应用,详细解释了逆序数的概念,并通过一个具体实例来演示如何使用树状数组高效求解逆序数问题。

http://acm.hdu.edu.cn/showproblem.php?pid=1394

树状数组求逆序数,逆序数个数为出现过的比当前的数字大的元素的个数,就是该数逆序数的个数,这里对于一个数a[i],所有已出现的数都是1,那么getsum(n),就是求所有已经出现过的数的个数,也可以是i,getsum(a[i])就是求所有比a[i]小的元素的个数,那么逆序数的个数就是i-getsum(a[i]);

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15131    Accepted Submission(s): 9241


Problem Description
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.

For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:

a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)

You are asked to write a program to find the minimum inversion number out of the above sequences.
 

Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
 

Output
For each case, output the minimum inversion number on a single line.
 

Sample Input
  
10 1 3 6 9 0 8 5 7 4 2
 

Sample Output
  
16
 

Author
CHEN, Gaoli
 

Source
 
#include <cstdio>
#include <cstring>
int a[5010];
int c[5010];//树状数组存储比i大的元素的个数
int n;
int bit(int t){
    return t&(-t);
}
void update(int pos,int num){
    while(pos<=n){
        c[pos]+=num;
        pos+=bit(pos);
    }
}
int getsum(int pos){
    int ans=0;
    while(pos>0){
        ans+=c[pos];
        pos-=bit(pos);
    }
    return ans;
}
int main(){
    while(scanf("%d",&n)!=EOF){
        memset(c,0,sizeof(c));
        int minnum=0;
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            a[i]++;
            minnum+=getsum(n)-getsum(a[i]);
            update(a[i],1);//使用树状数组你可以看成修改原数组的值就用update,求和就用getsum函数;
        }
        int m=minnum;
        for(int i=0;i<n;i++){
            m+=n-2*a[i]+1;
            if(m<minnum)minnum=m;
        }
        printf("%d\n",minnum);
    }
    return 0;
}



<think> 我们首先需要理解用户意图:用户询问"Inversion"的含义和相关信息。 由于"Inversion"一词在多个领域有不同含义,我们需要通过搜索确定最相关的信息,并覆盖主要领域。 根据常见情况,Inversion可能指: 1. 数学中的逆运算或反演 2. 遗传学中的倒位(染色体结构变异) 3. 气象学中的逆温现象 4. 化学中的转化(如蔗糖转化为果糖和葡萄糖) 5. 语言学中的倒装 6. 计算机科学中的反转(如数组反转) 但为了避免信息过载,我们将聚焦于最普遍和常见的解释,同时提供多个领域的简要说明。 我们将进行网络搜索,收集信息并整合。 注意:按照要求,回答应分方法或步骤,换行分隔,不使用步骤词汇,不集中引用,不使用第一人称。 计划结构: 1. 概述Inversion的多领域含义(作为引言) 2. 分领域解释(数学、遗传学、气象学、计算机科学等),每个领域作为一点,换行分隔 3. 每个领域下给出定义和关键信息,必要时给出例子(如公式或代码) 最后,根据这些信息生成相关问题。 </think> 经过网络搜索,Inversion在不同领域有以下核心含义和相关信息: ### 数学领域 矩阵逆运算:若矩阵$A$可逆,其逆矩阵$A^{-1}$满足$AA^{-1}=I$($I$为单位矩阵)。求解公式:$$A^{-1} = \frac{1}{\det(A)} \cdot \text{adj}(A)$$ 函数反演:针对函数$y=f(x)$,寻找反函数$x=f^{-1}(y)$,例如$y=e^x$的反函数为$x=\ln y$。 ### 遗传学领域 染色体倒位(Inversion):染色体片段发生180°旋转的重排现象,分为臂内倒位(不包含着丝粒)和臂间倒位(包含着丝粒)。可能降低生物繁殖力,是进化研究的重要标记。 ### 气象学领域 逆温现象:大气温度随高度增加而上升的反常层结(正常应下降),常见于晴朗夜间地表辐射冷却时。公式表达为:$$\frac{\partial T}{\partial z} > 0$$ 其中$T$为温度,$z$为高度。 ### 计算机科学领域 控制流反转:通过回调函数或事件驱动实现异步编程。示例代码: ```javascript // 事件监听实现控制反转 button.addEventListener('click', () => { console.log("事件触发反转控制流"); }); ``` 数据反转操作:常见于数组处理,Python实现: ```python arr = [1, 2, 3] reversed_arr = arr[::-1] # 输出 [3, 2, 1] ``` ### 化学领域 蔗糖转化:蔗糖水解为葡萄糖和果糖的反应($C_{12}H_{22}O_{11} + H_2O \rightarrow C_6H_{12}O_6 + C_6H_{12}O_6$),旋光度从正变负,故称"Inversion"。 ### 语言学领域 倒装结构:改变正常语序的修辞手法,如英语疑问句"Do you know?"(主语you后置)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值