HDU1394(线段树||树状数组)

Minimum Inversion Number

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


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
 

逆序数的概念: 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个 逆序 。一个排列中逆序的总数就称为这个排列的 逆序数 。逆序数为 偶数 的排列称为 偶排列 ;逆序数为奇数的排列称为 奇排列 。如2431中,21,43,41,31是逆序,逆序数是4,为偶排列。

逆序数计算方法是:在逐个元素读取原始数列时,每次读取都要查询当前已读取元素中大于当前元素的个数,加到sum里,最后得到的sum即为原始数列的逆序数。

接下来找数列平移后得到的最小逆序数,假设当前序列逆序数是sum,那么将a[0]移到尾部后逆序数的改变是之前比a[0]大的数全部与尾部a[0]组合成逆序数,假设数量为x,则x=n-1-a[0],而之前比a[0]小的数(也就是之前能和a[0]组合为逆序数的元素)不再与a[0]组合成逆序数,假设数量为y,则y=n-x-1,这样,新序列的逆序数就是sum+x-y=sum-2*a[0]+n-1;

接下来说明下线段树的作用,线段区间表示当前已读取的元素个数,比如[m,n]表示在数字m到n之间有多少个数已经读入,build时所有树节点全部为0就是因为尚未读数,ins函数是将新读入的数字更新到线段树里,点更新,query函数是查询当前数字区间已存在的数字个数。

 除去逆序数方面的内容,这基本就是一道线段树的模板题

//2016.8.10
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 5005
#define lson (id<<1)
#define rson ((id<<1)|1)
#define mid ((l+r)>>1)

using namespace std;

struct node
{
    int sum;
}tree[N*5];

int arr[N];

void build_tree(int id, int l, int r)
{
    tree[id].sum = 0;
    if(l == r){
        return ;
    }
    build_tree(lson, l, mid);
    build_tree(rson, mid+1, r);
    return;
}

void ins(int id, int l, int r, int pos)
{
    if(l == r){
        tree[id].sum = 1;return ;
    }
    if(pos <= mid)
      ins(lson, l, mid, pos);
    else 
      ins(rson, mid+1, r, pos);
    tree[id].sum = tree[lson].sum + tree[rson].sum;
    return ;
}

int query(int id, int l, int r, int ql, int qr)
{
    if(ql<=l&&r<=qr){
        return tree[id].sum;
    }
    int cnt = 0;
    if(ql<=mid)cnt += query(lson, l, mid, ql, qr);
    if(mid+1<=qr)cnt += query(rson, mid+1, r, ql, qr);

    return cnt;
}

int main()
{
    int n;
    while(cin>>n)
    {
        int sum = 0;
        build_tree(1, 0, n-1);
        for(int i = 0; i < n; i++){
            scanf("%d", &arr[i]);
            sum+=query(1, 0, n-1, arr[i], n-1);
            ins(1, 0, n-1, arr[i]);
        }
        //得到初始序列的逆序数
        int ans = sum;
        for(int i = 0; i < n; i++)//移动数列找最小逆序数
        {
            sum = sum-2*arr[i]+n-1;
            if(ans > sum)ans = sum;
        }
        cout<<ans<<endl;
    }

    return 0;
}

 树状数组

 1 //2016.9.13
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #define N 5005
 6 
 7 using namespace std;
 8 
 9 int a[N], arr[N], n;
10 
11 int lowbit(int x){return x&(-x);}
12 
13 void add(int pos, int tt)
14 {
15     for(int i = pos; i <= n; i+=lowbit(i))
16           arr[i] += tt;
17 }
18 
19 int query(int pos)
20 {
21     int sum = 0;
22     for(int i = pos; i > 0; i-=lowbit(i))
23           sum += arr[i];
24     return sum;
25 }
26 
27 int main()
28 {
29     int sum, ans, tmp;
30     while(scanf("%d", &n)!=EOF)
31     {
32         memset(arr, 0, sizeof(arr));
33         for(int i = 1; i <= n; i++)
34         {
35             scanf("%d", &a[i]);
36             a[i];
37         }
38         sum = 0;
39         for(int i = 1; i <= n; i++)
40         {
41             sum += query(n-a[i]);//n-a[i]表示a[i]为第n-a[i]大
42             add(n-a[i], 1);
43         }
44         ans = sum;
45         for(int i = 1; i < n; i++)
46         {
47             add(n-a[i], -1);
48             sum = sum+query(n-a[i])-a[i];
49             add(n-a[i], 1);
50             if(ans > sum) ans = sum;
51         }
52         printf("%d
", ans);
53     }
54 
55     return 0;
56 }
原文地址:https://www.cnblogs.com/Penn000/p/5758120.html