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): 19246    Accepted Submission(s): 11602

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:  1166 1698 1540 1542 1255

 

——————————————————正文分割线—————————————————————————————————

  最近在做线段树,在某个不知名的习题集里发现了这道题, 大概题意是给你一个序列,这个序列可以把前k(k<n)个数字移到后面生成新的一个序列,然后对于k从0到n-1而生成的序列里面找出其逆序对个数最小的一个数字。

  上手先拆分问题,首先是对于原来的序列找逆序对的过程,讲道理,因为这个n比较小,5000个,所以理论上说是直接可以暴力生成答案了,但是这样的解决方法太过局限,那不做考虑。

  然后想到之前也做过这样类似的问题,但是发现想不起来大概的解决细节,遂查询记录,找答案。然后发现当时使用树状数组解决问题的,事实上也可以用线段树解决,但是相比而言肯定是直接套树状数组的模板来的轻松得多。

  那么我们明确问题,怎么找一个逆序对?对于一个输入的数字,若之前有比它大的数字输入过,那么必然存在一个逆序对,那这样则可建立一个0-n-1的表,然后输入数字x的时候向后查找大于x的数有多少,然后把x对应的地方标1即可。(同理,也可以先输入数组,然后对于每个数向后查找,看看那些数是小于它的)

  这样就变成了一个求数组区间和的问题了,解决这个问题线段树和树状数组都可以。

  然后是第二个问题,也是本题的一个重点的关键,看到这个问题的时候,本人是迷茫的,的确,当k=1的时候,非常简单,因为后面的所有用a1作为第一元素的逆序对应该全部变成顺序对,顺序对全部变成逆序对,即sum=sum-s[a1]+n-1-s[a1],(s[a1]即把a1作为第一元素的逆序对的数量),但是这样一来,后面的序列存在着其s[ai]改变的可能,所以直接用原来的数据是不可能的,又不能更新一遍,由此陷入懵逼状态。

  无奈看了题解,然后发现一个十分尴尬的情况,既然每一个点是从第一个到最后一个,那么前面的数字只有两种可能,一种是小于它的,一种是大于它的,他们的数组分别是排序中在其前面的数量,即排序中在其后面的数量,那么排序在其后面的数字在a1在第一位的时候是顺序对,那么当a1变成倒数第1位的时候就一定是逆序对,同理,排序在其前的状况类似。

  非常巧,题目输入的值就是每个数字的排名,所以可以直接套来用,其实就算不是排名而是杂乱无章的数字,也可以用离散化来预处理一下以后套用,对于这道题的举一反三就是这样的了。

———————————————code——————————————————

 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<climits>
#include<map>
#include<stack>
#define file_in freopen("input.txt","r",stdin)
#define MAX 100005
#define maxn 5005
using namespace std;
#define LL long long
#define FF(x,y) for(int i=x;i<y;i++)
LL a[maxn];
int n;
LL lowbit(LL p) { return (p&-p); }
LL sum(LL p) {
    LL ret = 0;
    while (p> 0) ret += a[p], p -= lowbit(p);
    return ret;
}
void add(LL p, LL v) { // 若要减去,则v传入一个负数
    while (p <= n) a[p] += v, p += lowbit(p);
}

int main(void)
{
    while (scanf("%d", &n) != EOF)
    {
        vector<int>sto(n, 0);
        vector<int>ori(n);
        memset(a, 0, sizeof(a));
        LL s = 0;
        FF(0, n)
            scanf("%d", &ori[i]);
        for (int i = n - 1; i >= 0; i--)
        {
            int temp = ori[i];
            sto[i] = sum(temp + 1);
            s += sto[i];
            add(temp + 1, 1);
        }
        LL minn = s;
        FF(0, n)
        {
            s = s - ori[i] + n - 1 - ori[i];
            minn = min(minn, s);
        }
        cout << minn << endl;
    }
}

转载于:https://www.cnblogs.com/stultus/p/6354209.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值