程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu1394--Minimum Inversion Number(線段樹求逆序數,純為練習)

hdu1394--Minimum Inversion Number(線段樹求逆序數,純為練習)

編輯:C++入門知識

hdu1394--Minimum Inversion Number(線段樹求逆序數,純為練習)


Minimum Inversion Number

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


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題目大意,從初始的數組開始,每次把第一個加到最後一個,求逆序數,共求出n個逆序數,找出最小值對每種情況都求逆序數,可以每次都歸並排序,或樹狀數組,或線段樹,也可以由上一個的逆序數推出;a[1] , a[2] , a[3] 。。。a[n],將a[1]放到最後,然後將a[2]放到最後,可以找到規律,將首個a[i]放到最後時,逆序數增加了 a[i]之前比a[i]大的,增加a[i]之後比a[i]大的,減小了a[i]之前比a[i]小的,減小了a[i]之後比a[i]小的,又因為每次給出的數n個數在0到n,且都不同,最後得出 逆序數會增加 n-a[i]個,減少a[i]-1個

#include 
#include 
#define INF 0x3f3f3f3f
#include 
using namespace std;
int tree[100000] , p[6000] , q[6000];
void update(int o,int x,int y,int u)
{
    if( x == y && x == u )
        tree[o]++ ;
    else
    {
        int mid = (x + y)/ 2;
        if( u <= mid )
            update(o*2,x,mid,u);
        else
            update(o*2+1,mid+1,y,u);
        tree[o] = tree[o*2] + tree[o*2+1];
    }
}
int sum(int o,int x,int y,int i,int j)
{
    int ans = 0 ;
    if( i <= x && y <= j )
        return tree[o] ;
    else
    {
        int mid = (x + y) /2 ;
        if( i <= mid )
            ans += sum(o*2,x,mid,i,j);
        if( mid+1 <= j )
            ans += sum(o*2+1,mid+1,y,i,j);
    }
    return ans ;
}
int main()
{
    int i , j , n , min1 , num ;
    while(scanf("%d", &n)!=EOF)
    {
        min1 = 0 ;
        memset(q,0,sizeof(q));
        for(i = 1 ; i <= n ; i++)
        {
            scanf("%d", &p[i]);
            p[i]++ ;
        }
        memset(tree,0,sizeof(tree));
        for(i = 1 ; i <= n ; i++)
        {
            min1 += sum(1,1,n,p[i],n);
            update(1,1,n,p[i]);
        }
        num = min1 ;
        for(i = 1 ; i < n ; i++)
        {
            num = num + ( n - p[i] ) - (p[i] - 1) ;
            if( num < min1 )
                min1 = num ;
        }
        printf("%d\n", min1);
    }
    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved