Minimum Inversion Number - HDU 1394 树状数组

本文探讨了如何通过树状数组或线段树算法解决最小逆序数问题,即找到一系列数字排列中逆序对数量最少的情况。文章提供了完整的代码实现,并解释了核心算法思想。

Minimum Inversion Number

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


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
 

题意:每次求前面比后面大的对数,然后改变原序列顺序,取其中的最小值为答案。

思路:先用树状数组求出当前序列的对数,然后ret=ret-(num[i]-1)+(n-num[i]);线段树也可以。

树状数组AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,tree[5010],num[5010];
int lowbit(int x)
{
    return x&(-x);
}
void update(int pos)
{
    for(;pos<=n;pos+=lowbit(pos))
       tree[pos]++;
}
int query(int pos)
{
    int ret=0;
    for(;pos>0;pos-=lowbit(pos))
       ret+=tree[pos];
    return ret;
}

int main()
{
    int i,j,k,ans,ret;
    while(~scanf("%d",&n))
    {
        memset(tree,0,sizeof(tree));
        memset(num,0,sizeof(num));
        ret=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&k);
            k++;
            update(k);
            num[i]=k;
            ret+=i-1-query(k-1);
        }
        ans=ret;
        for(i=1;i<=n;i++)
        {
            ret=ret-(num[i]-1)+(n-num[i]);
            ans=min(ans,ret);
        }
        printf("%d\n",ans);
    }
}


线段树AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int num,l,r;
}tree[20010];
int val[5010];
void build(int o,int l,int r)
{
    tree[o].l=l;
    tree[o].r=r;
    tree[o].num=0;
    if(l==r)
      return;
    int mi=(l+r)>>1;
    build(o<<1,l,mi);
    build(o<<1|1,mi+1,r);
}
void update(int o,int pos)
{
    if(tree[o].l==pos && tree[o].r==pos)
    {
        tree[o].num=1;
        return;
    }
    int mi=(tree[o].l+tree[o].r)>>1;
    if(pos<=mi)
      update(o<<1,pos);
    else
      update(o<<1|1,pos);
    tree[o].num=tree[o<<1].num+tree[o<<1|1].num;
}
int query(int o,int l,int r)
{
    if(tree[o].l==l && tree[o].r==r)
      return tree[o].num;
    int mi=(tree[o].l+tree[o].r)>>1;
    if(r<=mi)
      return query(o<<1,l,r);
    if(l>mi)
      return query(o<<1|1,l,r);
    return query(o<<1,l,mi)+query(o<<1|1,mi+1,r);
}
int main()
{
    int n,i,j,k,ret,ans;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&val[i]);
            val[i]++;
        }
        build(1,1,n);
        ret=0;
        for(i=1;i<=n;i++)
        {
            ret+=query(1,val[i],n);
            //printf("%d\n",ret);
            update(1,val[i]);
        }
        ans=ret;
        //printf("%d\n",ret);
        for(i=1;i<=n;i++)
        {
            ret=ret-(val[i]-1)+(n-val[i]);
            //printf("%d\n",ret);
            ans=min(ans,ret);
        }
        printf("%d\n",ans);
    }
}




### 实现一维数组的逆置算法 在一维数组的逆置过程中,主要思路是交换首尾元素并逐步向中间靠拢直到完成整个数组的反转。以下是几种不同的方法来实现这一功能。 #### 使用 `while` 循环与双指针法 这种方法利用两个指向数组两端的指针,在每次迭代中交换这两个位置上的值,并使左指针右移一位而右指针左移一位,直至两者相遇或交错为止[^2]。 ```c++ void Inversion1(int a[], int sz) { int temp = 0; int* left = a; int* right = a + sz - 1; while (left < right) { temp = *left; *left = *right; *right = temp; left++; right--; } } ``` 此函数接收一个整型数组及其大小作为参数,内部采用临时变量存储待交换的数据项以防止数据丢失。 #### 利用索引来遍历数组 另一种常见的做法就是直接基于下标的访问来进行元素间的替换工作。这种方式不需要额外声明指针类型的变量,而是通过调整索引的方式达到相同的效果[^3]。 ```cpp #include <stdio.h> int main() { int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; // 定义数组 int i = 0; int j = sizeof(arr) / sizeof(arr[0]) - 1; while (i < j) { int temp = arr[i]; // 定义一个临时变量temp arr[i] = arr[j]; arr[j] = temp; i++; j--; } for (int k = 0; k < 10; k++) { printf("%d\n", arr[k]); } return 0; } ``` 上述代码片段展示了完整的程序逻辑,不仅包含了核心的逆转过程,还包括了初始化阶段以及最终的结果展示部分。 #### 应用指针简化操作 当涉及到更复杂的场景时,比如动态分配内存或者处理多维结构体等形式下的数组时,则可以考虑引入更多高级特性如指针运算等手段进一步优化性能表现[^4]。 ```c++ #define N 10 int main() { int a[N], t; for(t=0;t<N;t++){ scanf("%d",&a[t]); } int *start = &a[0], *end = &a[N-1]; while(start < end){ t = *start; *start++ = *end; *end-- = t; } for(t=0;t<N;t++){ printf("%d ",a[t]); } return 0; } ``` 这段源码实现了用户交互式的输入输出流程,允许从标准输入读取一系列数值填充到指定长度的一维整形数组当中去;接着调用了之前提到过的双指针技术完成了实际意义上的翻转动作;最后再次打印出经过变换后的序列供验证之用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值