天天看點

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): 18375    Accepted Submission(s): 11156

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

​​ZOJ Monthly, January 2003​​

Recommend

Ignatius.L   |   We have carefully selected several similar problems for you:  ​​1698​​​ ​​1540​​​ ​​1542​​​ ​​1255​​​ ​​2795​​ 

題解:問你不斷地将開頭的數放到結尾。求其中最小的逆序數。

線段樹求出最初的逆序數。複雜度O(nlogn),然後O(n)查詢其他解。

AC代碼:

#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <bitset>
#include <iomanip>
#include <list>
#include <stack>
#include <utility> 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
const double eps = 1e-8;  
const int INF = 1e9+7; 
const ll inf =(1LL<<62) ;
const int MOD = 1e9 + 7;  
const ll mod = (1LL<<32);
const int N =1e6+6; 
const int M=100010;
const int maxn=55555;
#define mst(a) memset(a, 0, sizeof(a))
#define M_P(x,y) make_pair(x,y)
#define in freopen("in.txt","r",stdin) 
#define rep(i,j,k) for (int i = j; i <= k; i++)  
#define per(i,j,k) for (int i = j; i >= k; i--)  
#define lson  l , mid , rt << 1    
#define rson  mid + 1 , r , rt << 1 | 1  
const int lowbit(int x) { return x&-x; }
//const int lowbit(int x) { return ((x)&((x)^((x)-1))); } 
int read(){ int v = 0, f = 1;char c =getchar();
while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}
while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();
return v*f;}
int sum[maxn<<2];
int x[maxn];
void pushup(int rt) //把目前結點的資訊更新到父結點
{
    //線段樹是用數組來模拟樹形結構
    //對于每一個節點rt,左子節點為 2*rt (一般寫作rt<<1)右子節點為 2*rt+1(一般寫作rt<<1|1) 
    sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 
}
void build(int l,int r,int rt)
{
    sum[rt] = 0;
    if(l==r) return ; 
    int mid=(l+r)>>1;
    build(lson);//遞歸構造左子樹
    build(rson);//遞歸構造右子樹
    pushup(rt); //更新求和 
}
void update(int p, int l, int r, int rt)//單點增 
{
    if(l==r){
        sum[rt] ++; return ;
    }
    int mid=(l+r)>>1;
    if(p<=mid) update(p,lson);
    else update(p,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt) //區間求逆序數
{
    if(L <= l && r <= R){
        return sum[rt];
    }
    int mid = (l+r)>>1;
    int ans = 0;
    if(L <= mid) ans+=query(L,R,lson);
    if(R > mid) ans+=query(L,R,rson);
    return ans; 
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        build(0,n-1,1);
        int sum = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&x[i]);
            sum+=query(x[i],n-1,0,n-1,1);
            update(x[i],0,n-1,1);
        }
        int ans = sum;
        for(int i=0;i<n;i++)
        {
            sum=sum-x[i]; //減少了的逆序數 
            sum+=n-x[i]-1; //增加了的逆序數 
            ans=min(ans,sum);
        }
        cout<<ans<<endl;
     } 
    return 0;
}