zoukankan      html  css  js  c++  java
  • Minimum Inversion Number~hdu 1394

    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. 

    InputThe 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.
    OutputFor 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


    这题先要补充一个逆序数的概念
    在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,
    那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。
    一个排列中所有逆序总数叫做这个排列的逆序数。也就是说,对于n个不同的元素,
    先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规定从小到大为标准次序),
    于是在这n个元素的任一排列中,当某两个元素的先后次序与标准次序不同时,
    就说有1个逆序。一个排列中所有逆序总数叫做这个排列的逆序数。
    (以上文字引用自百度百科)
    由于数据较大求逆序数就必须用复杂度较少的方法求,强行暴力求逆序数表示应该没人会选择这样做吧。
    线段树就是一个非常好的数据结构用于求逆序数。
    题意:一个有N个数的序列中,每次把第一个放在序列的最后一个形成新的序列,
    求这些序列中最小的逆序数是多少?
    求出原始序列的逆序数S,将a[i]移到最后一位时,新的序列的逆序数为
    原来序列的逆序值S+比a[i]大的数的个数(n-1-a[i])-a[i](比a[i]小的个数)
    如果这题本菜鸟有什么地方理解不正确,请读者在下方给我留言,纠正我的错误。
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 #define maxn 5010
     7 int  sum[maxn*4+10],a[maxn];
     8 void pushup(int rt)
     9 {
    10      sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    11 }
    12 void build(int l,int r,int rt)
    13 {
    14     sum[rt]=0;
    15     if (l==r) return ;
    16     int m=(l+r)>>1;
    17     build(l,m,rt<<1);
    18     build(m+1,r,rt<<1|1);
    19 }
    20 void updata(int x,int l,int r,int rt )
    21 {
    22     if (l==r) {
    23         sum[rt]++;
    24         return ;
    25     }
    26     int m=(l+r)>>1;
    27     if (x<=m) updata(x,l,m,rt<<1);
    28     else updata(x,m+1,r,rt<<1|1);
    29     pushup(rt);
    30 }
    31 int query(int x,int y,int l,int r,int rt)
    32 {
    33     if (x<=l && r<=y) return sum[rt];
    34     int m=(l+r)>>1;
    35     int ans=0;
    36     if (x<=m) ans+=query(x,y,l,m,rt<<1);
    37     if (y>m)  ans+=query(x,y,m+1,r,rt<<1|1);
    38     return ans;
    39 }
    40 int main()
    41 {
    42     int n;
    43     while(scanf("%d",&n)!=EOF){
    44         build(0,n-1,1);
    45         int s=0;
    46         for (int i=0 ;i<n ;i++){
    47             scanf("%d",&a[i]);
    48             s+=query(a[i],n-1,0,n-1,1);
    49             updata(a[i],0,n-1,1);
    50         }
    51         int ans=s;
    52         for (int i=0 ;i<n ;i++ ){
    53             s+=(n-a[i]-1)-a[i];
    54             ans=min(ans,s);
    55         }
    56         printf("%d
    ",ans);
    57     }
    58     return 0;
    59 }
  • 相关阅读:
    算法常识——二叉堆
    关于c++ 感想
    算法常识——树的遍历
    算法常识——非线性结构
    算法常识——基础的数据结构
    算法常识——结构与复杂度
    重温网络编程——常识(三)
    重温网络编程——协议(二)
    重温网络编程(一)
    RemoteViews 整理
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/8506934.html
Copyright © 2011-2022 走看看