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
看了别人的代码做出来了。。 这题先用线段树的方法把读入数据的逆序值求出来,依次插入点,每插入一个点k就看[k+1,n]中前面插入的点有几个,即为该点的逆序值,然后再更新从根线段到[k,k]这些线段的值。点都插入完毕后,再一次把最前面的点移到最后面,此时总逆序数会减少a[i]-1,增加n-a[i],然后求出最小的逆序数就行了。

#include<stdio.h>
#include<string.h>
#define maxn 5005
int a[maxn];
struct
node
{
int
l,r,sum;
}
b[4*maxn];
void
build(int l,int r,int i)
{
int
mid;
b[i].l=l;
b[i].r=r;
b[i].sum=0;
if(
l==r)
return;

mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
} int
inverse(int l,int r,int i)
{
int
mid;
if(
b[i].l==l && b[i].r==r)
{
return
b[i].sum;
}

mid=(b[i].l+b[i].r)/2;
if(
l>mid)
return
inverse(l,r,i*2+1);
else if(
r<=mid)
return
inverse(l,r,i*2);
else if(
r>mid && l<=mid)
return
inverse(l,mid,i*2)+inverse(mid+1,r,i*2+1);
} void
change(int i,int id)
{
int
mid;
if(
b[i].l==id && b[i].r==id)
{

b[i].sum=1;
return;
}

mid=(b[i].l+b[i].r)/2;
if(
id<=mid)
change(i*2,id);
else if(
id>mid)
change(i*2+1,id);
b[i].sum=b[i*2].sum+b[i*2+1].sum;
} int main()
{
int
n,m,i,j,ans,min;
while(
scanf("%d",&n)!=EOF)
{

build(1,n,1);
ans=0;
for(
i=1;i<=n;i++)
{

scanf("%d",&a[i]);
ans=ans+inverse(a[i]+1,n,1);
change(1,a[i]);
}

min=ans;
for(
i=1;i<=n;i++)
{

ans=ans+n-2*a[i]-1;
if(
ans<min)
min=ans;
}

printf("%d\n",min);
}
}

hdu1394Minimum Inversion Number的更多相关文章

  1. HDU1394-Minimum Inversion Number

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number Time Limit: 2000/1000 MS (Ja ...

  2. hdu1394--Minimum Inversion Number(线段树求逆序数,纯为练习)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  3. hdu1394Minimum Inversion Number(线段树,求最小逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  4. 线段树---HDU1394Minimum Inversion Number

    此题和上题略有不同,但是大体差不多,不过要把题意转换过来,题目大体意思为, 输入n, 也就是n个数,这些数为0 - (n-1), 这些数刚开始给定输入的顺序, 然后求他的逆序数,然后接着把第一个移到这 ...

  5. C++-HDU1394-Minimum Inversion Number[数据结构][树状数组]

    给出0~n-1的一个排列,可以整体移动,求逆序对最小值 把数字num[i]的加入,等价于树状数组的第n-num[i]位加1 因为num[i]是第 (n-1)-num[i]+1=n-num[i]大的数字 ...

  6. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

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

  7. HDU 1394 Minimum Inversion Number(最小逆序数 线段树)

    Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...

  8. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  9. ACM Minimum Inversion Number 解题报告 -线段树

    C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

随机推荐

  1. Python模块化编程与装饰器

    Python的模块化编程 我们首先以一个例子来介绍模块化编程的应用场景,有这样一个名为requirements.py的python3文件,其中两个函数的作用是分别以不同的顺序来打印一个字符串: # r ...

  2. appium识别工具介绍

  3. Java 在pom.xml中配置build resources, 来防止我们资源导出失败问题(Maven项目)

    在pom.xml中配置build, 来防止我们资源导出失败问题 <!--在build中配置resources, 来防止我们资源导出失败问题--> <build> <res ...

  4. ORA-12560错误

    ora-12560错误是一个经典错误之一 下面我们分析一下这个错误: 产生这个错误的原因是什么呢? 1.oracle服务没有启动 Linux下查看$ps -ef | grep ora_ windows ...

  5. 【CRS】vipca最后一步执行报错CRS-0215

    当我们在安装Clusterware 的时候, 需要在第二节点上vipca , 配置到最后安装的时候, 安装到 75% 左右,报错:     CRS-0215 : Could not start res ...

  6. VGA调试心得

    以前自己调试过视频信号,无非就时钟加行场同步加数据线,如果视频信号出问题,第一看现象,第二测频率,反正出问题不是消隐信号出问题,就是时钟频率出问题.通过这种方式也调试成功过几个显示屏,然后就以为自己对 ...

  7. Spark SQL如何选择join策略

    前言 众所周知,Catalyst Optimizer是Spark SQL的核心,它主要负责将SQL语句转换成最终的物理执行计划,在一定程度上决定了SQL执行的性能. Catalyst在由Optimiz ...

  8. I/O 复用 multiplexing data race 同步 coroutine 协程

    小结: 1.A file descriptor is considered ready if it is possible to perform the corresponding I/O opera ...

  9. automake的简单使用

    https://blog.csdn.net/zhengqijun_/article/details/70105077 xxxxx https://blog.csdn.net/initphp/artic ...

  10. FFT,NTT 笔记

    FFT 简介 FFT是干啥的?它是用来加速多项式乘法的.我们平时经常求多项式乘法,比如\((x+1)(x+3)=(x^2+4x+3)\).假设两个式子都是\(n\)项(不足的补0),那朴素的算法是\( ...