线段树——求逆序对hdu1394

Minimum Inversion Number

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

题意:求一个排列的逆序对数,然后移动这个排列得到的新排列最小的逆序对

模板题

线段树区间内数字出现的次数和,当加入一个a[ i ]时查询他右边的数字出现的次数;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
const int maxn=5005;
int n;
int b[maxn<<2];
int a[maxn];
void build(int l,int r,int rt)
{
    b[rt]=0;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(l,mid,rt*2);
    build(mid+1,r,rt*2+1);
}
void add(int l,int r,int rt,int v)
{
    b[rt]+=1;
    if(l==r)
    {
        return ;
    }
    int mid=(l+r)>>1;
    if(v<=mid)    add(l,mid,rt*2,v);
    else    add(mid+1,r,rt*2+1,v);
}
int sum(int l,int r,int rt,int ll,int rr)
{
    if(ll<=l&&r<=rr) return b[rt];
    int mid=(l+r)>>1;
    int ans=0;
    if(ll<=mid)     ans+=sum(l,mid,rt<<1,ll,rr);
    if(rr>mid)    ans+=sum(mid+1,r,rt<<1|1,ll,rr);
    return ans;
}
int main()
{
    int v;
    while(~scanf("%d",&n))
    {
        build(1,n,1);
        int ans=0;
        for(int i=1;i<=n;++i)
            {
                scanf("%d",&a[i]);
                ++a[i];
            }
        for(int i=1;i<=n;i++)
        {
            ans+=sum(1,n,1,a[i],n);//1到a[i]右边界的元素个数即是所求
            add(1,n,1,a[i]);//a[i]右边界

        }
        int mmin=ans;
        for(int i=1;i<=n;++i)
        {
            ans-=a[i]-1;
            ans+=n-a[i];
            
            mmin=min(ans,mmin);
        }
        cout<<mmin<<endl;
    }
    return 0;
}

 

分治算法逆序对是一种高效的方法。逆序对是指在一个序列中,若 $i < j$ 且 $a_j > a_i$,则 $(a_i, a_j)$ 构成一个逆序对 [^1]。 分治算法的核心思想是将一个大问题分解为多个小问题,分别解决这些小问题,然后将小问题的解合并得到原问题的解。在逆序对时,常结合归并排序来实现。 归并排序的过程中,在合并两个已排序的子数组时,可以统计逆序对的数量。具体步骤如下: 1. **分解**:将数组分成两个子数组,分别对这两个子数组递归地进行排序和逆序对统计。 2. **合并**:在合并两个已排序的子数组时,若发现左边子数组的元素大于右边子数组的元素,则说明存在逆序对。由于左边子数组已经有序,那么该元素及其后面的所有元素与右边当前元素都构成逆序对。 以下是使用分治算法逆序对的代码示例: ```cpp #include<bits/stdc++.h> #include<iostream> #include<cstdlib> #include<cstdio> using namespace std; int a[1000], hb[1000], sum, n; void gbpx(int l, int r) { int i, j, mid, z; mid = (l + r) / 2; if (l < r) { gbpx(l, mid); gbpx(mid + 1, r); } i = l; j = mid + 1; z = l; while (i <= mid && j <= r) { if (a[i] <= a[j]) { hb[z] = a[i]; z++; i++; } else { hb[z] = a[j]; z++; j++; sum += (mid - i + 1); } } while (i <= mid) { hb[z] = a[i]; i++; z++; } while (j <= r) { hb[z] = a[j]; j++; z++; } for (int d = l; d <= r; d++) a[d] = hb[d]; } int main() { freopen("gb.in", "r", stdin); freopen("gb.out", "w", stdout); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; gbpx(1, n); cout << sum << endl; fclose(stdin); fclose(stdout); return 0; } ``` 该算法的时间复杂度为 $O(n log n)$,这是因为分治过程中每次将问题规模减半,递归深度为 $log n$,而每次合并操作的时间复杂度为 $O(n)$ [^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值