前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU-1394 Minimum Inversion Number(线段树求逆序数)

HDU-1394 Minimum Inversion Number(线段树求逆序数)

作者头像
ShenduCC
发布2018-04-25 17:29:34
1K0
发布2018-04-25 17:29:34
举报
文章被收录于专栏:算法修养

Minimum Inversion Number

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

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

这道题目有两个地方可以留意一下。 首先求一个数列的逆序数 首先将这个数列排序,然后从最后一个数,边询问边更新 第二,根据题意,将第一个数放到最后一个,形成的新数列应该怎么求逆序数。其实可以递推,和没有变化前的数列比较逆序数增加了(n-1-a[i])-a[i]

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>

using namespace std;
#define MAX 5000
int segTree[MAX*4+5];
int n;
int a[MAX];
int b[MAX];
int c[MAX];
void PushUp(int node)
{
    segTree[node]=segTree[node<<1]+segTree[node<<1|1];
}
void build(int node,int begin,int end)
{
    if(begin==end)
    {
        segTree[node]=0;
        return;
    }
    int m=(begin+end)>>1;
    build(node<<1,begin,m);
    build(node<<1|1,m+1,end);
    PushUp(node);
}
void Update(int node,int begin,int end,int ind,int num)
{
    if(begin==end)
    {
        segTree[node]+=num;
        return;
    }
    int m=(begin+end)>>1;
    if(ind<=m)
        Update(node<<1,begin,m,ind,num);
    else
        Update(node<<1|1,m+1,end,ind,num);
    PushUp(node);
}
int Query(int node,int begin,int end,int left,int right)
{
    if(left<=begin&&end<=right)
        return segTree[node];
    int ret;
    int m=(begin+end)>>1;
    ret=0;
    if(left<=m)
        ret+=Query(node<<1,begin,m,left,right);
    if(right>m)
        ret+=Query(node<<1|1,m+1,end,left,right);
    return ret;
}
int cmp(int x,int y)
{
    return x<y;
}
int main()
{
    int sum;
    while(scanf("%d",&n)!=EOF)
    {
        sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            c[i]=a[i];
            b[a[i]]=i;
        }
        build(1,1,n);
        sort(a+1,a+n+1,cmp);
        for(int i=n;i>=1;i--)
        {
            sum+=Query(1,1,n,1,b[a[i]]);
            Update(1,1,n,b[a[i]],1);
        }
        int num;
        num=sum;
        int ans;
        ans=sum;
        for(int i=1;i<n;i++)
        {
            num+=-c[i]+(n-1-c[i]);
            if(ans>num)
                ans=num;
        }
        printf("%d\n",ans);


    }
    return 0;
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-12-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档