zoukankan      html  css  js  c++  java
  • 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): 15247    Accepted Submission(s): 9311


    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   |   We have carefully selected several similar problems for you:  1698 1540 1542 1255 2795 
     
     
    虽然在学线段树。。。但是感觉美必要为了用线段树而用线段树。。。
    我知道树状数组的东西都可以用线段树来解决。。。。
    不过还是觉得树状数组。。。实在是优美。。。所以这题还是用树状数组搞得。。
     
    这题是问一个长度为n的循环数组中,逆序对最少的个数。。。
    我们可以先用树状数组求出初始的数列的逆序对。。。
    然后其他的可以通过递推得到。。。。
    当a[i]从处于位置1而被放到最后的时候。。。
    cnt = cnt -a[i]+n-a[i]+1;
    然后取所有cnt的最大值就行。
     1 /*************************************************************************
     2     > File Name: code/hud/1394.cpp
     3     > Author: 111qqz
     4     > Email: rkz2013@126.com 
     5     > Created Time: 2015年10月28日 星期三 21时16分47秒
     6  ************************************************************************/
     7 
     8 #include<iostream>
     9 #include<iomanip>
    10 #include<cstdio>
    11 #include<algorithm>
    12 #include<cmath>
    13 #include<cstring>
    14 #include<string>
    15 #include<map>
    16 #include<set>
    17 #include<queue>
    18 #include<vector>
    19 #include<stack>
    20 #include<cctype>
    21                  
    22 #define yn hez111qqz
    23 #define j1 cute111qqz
    24 #define ms(a,x) memset(a,x,sizeof(a))
    25 using namespace std;
    26 const int dx4[4]={1,0,0,-1};
    27 const int dy4[4]={0,-1,1,0};
    28 typedef long long LL;
    29 typedef double DB;
    30 const int inf = 0x3f3f3f3f;
    31 const int N=5E3+7;
    32 int c[N];
    33 int n,a[N];
    34 
    35 int lowbit( int x)
    36 {
    37     return x&(-x);
    38 }
    39 
    40 
    41 void update ( int x,int delta)
    42 {
    43    // cout<<"a?"<<endl;
    44     for ( int i = x ; i <= n ; i = i + lowbit(i)) c[i] = c[i] + delta;
    45 }
    46 int Sum( int x)
    47 {
    48  //   cout<<"b?"<<endl;
    49     int res = 0 ;
    50     for ( int i = x ;i >= 1; i = i - lowbit(i))
    51     {
    52     res = res + c[i];
    53     }
    54     return res;
    55 }
    56 int main()
    57 {
    58   #ifndef  ONLINE_JUDGE 
    59    freopen("in.txt","r",stdin);
    60   #endif
    61    while (scanf("%d",&n)!=EOF)
    62     {
    63     ms(c,0);
    64     for ( int i = 1 ; i <= n ; i++)
    65     {
    66         scanf("%d",&a[i]);
    67         a[i]++;
    68     }
    69     int cnt = 0 ;
    70     int ans = inf;
    71  //   puts("mmmmmmmiao?");
    72     for ( int i = 1  ; i <= n ; i++)
    73     {
    74     update(a[i],1);
    75     cnt = cnt + i - Sum(a[i]);
    76 //    puts("whats wrong?");
    77     }
    78   //  printf("cnt::%d
    ",cnt);
    79     if (cnt<ans) ans = cnt;
    80     for ( int i = 1 ; i <= n ; i++)
    81     {
    82     cnt =cnt -a[i]+n-a[i]+1;
    83     if (cnt<ans&&cnt>0) ans = cnt;
    84 //    printf("cnt:%d
    ",cnt);
    85     }
    86     printf("%d
    ",ans);
    87     }
    88     
    89   
    90    
    91  #ifndef ONLINE_JUDGE  
    92   fclose(stdin);
    93   #endif
    94     return 0;
    95 }
    View Code
     
  • 相关阅读:
    python+selenium之页面元素截图
    selenium八大定位
    http概述之URL与资源
    数组中只出现一次的数字
    数字在排序数组中出现的次数
    把数组排成最小的数
    数组中出现次数超过一半的数字
    调整数组顺序使得奇数位于偶数的前面
    旋转数组的最小值
    二维数组的查找
  • 原文地址:https://www.cnblogs.com/111qqz/p/4918837.html
Copyright © 2011-2022 走看看