HDU 1394 Minimum Inversion Number [线段树->单点更新]【数据结构】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394

————————————————–.

Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18798 Accepted Submission(s): 11367

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

—————————————————.

题目大意:
就是有一个序列 ,这个序列可以循环,变成下列这么些个序列。
a1, a2, …, an-1, an (m = 0 - the initial seqence)
a2, a3, …, an, a1 (m = 1)
a3, a4, …, an, a1, a2 (m = 2)

an, a1, a2, …, an-1 (m = n-1)
然后对于每种序列 计算逆序对数
然后输出逆序对数最小的值

解题思路:
首先对于最初的序列求解逆序对数 ,只需要线段树or树状数组就行了
这里采取的是线段树维护

在每次把数据挂到树上之前 可以区间查询的方式计算这个值的逆序数 然后O( n )遍历一遍就能知道整个序列的逆序数了
总复杂度是O(nlogn)

然后他要求的序列一共有n个那样的话不能O( n2logn ) 这样复杂度实在太高了

然后想最后发现了有一个规律;
因为序列中的数就是0~n-1且新的序列是从旧的序列移过来的。
那么逆序数就会相应减少 ai 个 同时就增加了 nai1
这样的话就能够O( 1 <script type="math/tex" id="MathJax-Element-167">1</script>)处理了

附本题代码
————————————.

#include <bits/stdc++.h>

#define abs(x)          (((x)>0)?(x):-(x))
#define lalal           puts("*********")
#define Rep(a,b,c)      for(int a=(b);a<=(c);a++)
#define Req(a,b,c)      for(int a=(b);a>=(c);a--)
#define Rop(a,b,c)      for(int a=(b);a<(c);a++)
#define s1(a)           scanf("%d",&a)
typedef long long int LL;
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 9901;
/**************************************/
const int N = 10000+5;
struct node
{
    int l,r;
    int val;
    int md()
    {
        return (l+r)>>1;
    }
}tree[N<<2];
int a[N],ans;
#define ll  (rt<<1)
#define rr  (rt<<1|1)
#define mid (tree[rt].md())
void pushup(int rt)
{
    tree[rt].val=tree[ll].val+tree[rr].val;
}
void build(int rt,int l,int r)
{
    tree[rt].l=l,tree[rt].r=r;
    if(l==r)
    {
        tree[rt].val=0;
        return ;
    }
    build(ll,l,mid);
    build(rr,mid+1,r);
    pushup(rt);

    return;
}

void update(int rt,int pos,int val)
{
    if(tree[rt].l==tree[rt].r)
    {
        tree[rt].val+=val;
        return;
    }

    if(pos<=mid)update(ll,pos,val);
    else      update(rr,pos,val);
    pushup(rt);
    return;
}

void query(int rt,int L,int R)
{
    if(L<=tree[rt].l&&tree[rt].r<=R)
    {
        ans += tree[rt].val;
        return;
    }
    if(R<=mid)
        query(ll,L,R);
    else if(L>mid)
        query(rr,L,R);
    else
    {
        query(ll,L,R);
        query(rr,L,R);
    }
    return;
}

int main()
{
    int n;
    while(~s1(n))
    {
        build(1,0,n);

        int sum = 0;
        Rep(i,1,n)
        {
            s1(a[i]);
            ans = 0;
            query(1,a[i]+1,n);
            sum+=ans;
            update(1,a[i],1);
        }

        int mi = sum;
        Rep(i,1,n)
        {
            sum+=n-a[i]-a[i]-1;
            if(sum<mi)
                mi=sum;
        }
        printf("%d\n",mi);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值