• [HDU] 1394 Minimum Inversion Number [线段树求逆序数]


    Minimum Inversion Number

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


    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
     
    Recommend
    Ignatius.L
     
    题解:这题的维护信息为每个数是否已经出现。每次输入后,都从该点的值到n-1进行查询,每次发现出现了一个数,由于是从该数的后面开始找的,这个数肯定是比该数大的。那就是一对逆序数,然后逆序数+1.最后求完所有的逆序数之后,剩下的就可以递推出来了。因为假如目前的第一个数是x,那当把他放到最后面的时候,少的逆序数是本来后面比他小的数的个数。多的逆序数就是放到后面后前面比他大的数的个数。因为所有数都是从0到n-1.所以比他小的数就是x,比他大的数就是n-1-x。这样的话每次的逆序数都可以用O(1)的时间计算出来。然后找最小的时候就可以了。
     
     1 #include<cstdio>
     2 #include<algorithm>
     3 
     4 using namespace std;
     5 
     6 #define  lson  l , m , rt << 1
     7 #define  rson  m + 1 , r , rt << 1 | 1
     8 
     9 const int maxn = 5000 + 311;
    10 int sum[maxn<<2];
    11 
    12 
    13 void PushUp(int rt) 
    14 {
    15     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    16 }
    17 
    18 void build(int l,int r,int rt)
    19 {
    20     int m;
    21     
    22     sum[rt]=0;
    23     if(l==r) {
    24         return;
    25     }
    26     m=(l+r)>>1;
    27     build(lson);
    28     build(rson);
    29 }
    30 
    31 int query(int L,int R, int l, int r,int rt)
    32 {
    33     int m,ret;
    34     
    35     if(L<=l && r<=R) {
    36         return sum[rt];
    37     }
    38     
    39     m=(l+r) >> 1;
    40     ret=0; 
    41     if(L<=m) ret+=query(L,R,lson);
    42     if(R>m) ret+=query(L,R,rson);
    43     
    44     return ret;
    45 } 
    46 
    47 void Updata(int p,int l,int r,int rt)
    48 {
    49     int m;
    50     if (l==r) {
    51         sum[rt]++;
    52         return ;
    53     }
    54     m=(l+r)>>1;
    55     if(p<=m) Updata(p,lson);
    56     else Updata(p,rson);
    57     
    58     PushUp(rt); 
    59 }
    60 
    61 int main()
    62 {
    63     int n,sum,x[maxn];
    64     
    65     while(~scanf("%d",&n)) {
    66         sum=0;
    67         build(0,n-1,1);
    68         
    69         for(int i = 0;i < n;i++) {
    70             scanf("%d",&x[i]);
    71             sum+=query(x[i],n-1,0,n-1,1);
    72             Updata(x[i],0,n-1,1);
    73         }
    74         int ret=sum;
    75         for(int i=0;i<n;i++) {
    76             sum+=n-x[i]-x[i]-1;
    77             ret=min(ret,sum);
    78         }
    79         
    80         printf("%d
    ",ret);
    81     }
    82     
    83     return 0;
    84 }
  • 相关阅读:
    利用接口实现简单工厂模式
    简单工厂代码演示
    创建对象的三种方式
    eclipse中常用快捷键
    glog功能介绍
    sublime操作
    caffe train c++
    各层参数介绍,尤其数据层
    LSTM长短期记忆网络
    caffe c++
  • 原文地址:https://www.cnblogs.com/sxiszero/p/4125847.html
Copyright © 2020-2023  润新知