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

前端之家收集整理的这篇文章主要介绍了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,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,an-1,an (where m = 0 - the initial seqence)
a2,a3,a1 (where m = 1)
a3,a4,a1,a2 (where m = 2)

an,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,an (m = 0 - the initial seqence)
a2,a1 (m = 1)
a3,a2 (m = 2)

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

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

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

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

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

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

#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,c) for(int a=(b);a>=(c);a--)
#define Rop(a,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,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,R);
    else
    {
        query(ll,R);
        query(rr,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,n)
        {
            sum+=n-a[i]-a[i]-1;
            if(sum<mi)
                mi=sum;
        }
        printf("%d\n",mi);
    }
    return 0;
}
原文链接:https://www.f2er.com/datastructure/382404.html

猜你在找的数据结构相关文章