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. Selenium WebDriver 定位之Xpath定位

    Selenium 定位之Xpath定位: 1.绝对路径定位:以/开头从根节点一直找到当前节点,不推荐使用决定路径定位方式 2.相对路径定位:使用"//"表示相对路径定位,格式:// ...

  2. Docker 镜像基础(三)

    基于Dockerfile制作yum版本nginx镜像 [root@node-2 ~]# mkdir /opt/nginx [root@node-2 ~]# cd /opt/nginx/ ## 创建Do ...

  3. unixbench性能测试跑分工具

    UnixBench是一个类unix系(Unix,BSD,Linux)统下的性能测试工具,一个开源工具,被广泛用与测试linux系统主机的性能 所谓跑分工具,不仅各项的测试有得分,最后跑完也会有一个综合 ...

  4. awk中的if ,else

    local pct="$(awk -v one="$1" -v two="$2" 'BEGIN{ if (two > 0) { printf & ...

  5. ctfshow—web—web6

    打开靶机 发现登录窗,首先想到SQL注入 抓包,进行SQL注入测试 测试发现空格符被过滤了 使用/**/代替空格符进行绕过,绕过后登录成功 检测回显位 开始查询数据库名 开始查询数据库内数据表名称 查 ...

  6. SSTI

    最牛bypass:https://blog.csdn.net/solitudi/article/details/107752717 SSTI的奇怪绕过姿势:https://blog.csdn.net/ ...

  7. kafka(三)原理剖析

    一.生产者消息分区机制原理剖析 在使用Kafka 生产和消费消息的时候,肯定是希望能够将数据均匀地分配到所有服务器上.比如很多公司使用 Kafka 收集应用服务器的日志数据,这种数据都是很多的,特别是 ...

  8. Hadoop2.7.7阿里云安装部署

    阿里云的网络环境不需要我们配置,如果是在自己电脑上的虚拟机,虚拟机的安装步骤可以百度.这里是单机版的安装(也有集群模式的介绍)使用Xshell连接阿里云主机,用命令将自己下载好的安装包上传到服务器 # ...

  9. windows中使用django时报错:A server error occurred. Please contact the administrator.

    这是因为在视图函数中使用了get函数,获取了不存在的数据例如:数据库中不存在一条name为hello1的数据,使用如下语句访问message = Message.objects.get(name='h ...

  10. Mysql数据库下InnoDB数据引擎下的事务详解

    一.什么是数据库事务? 数据库事务( transaction)是访问并可能操作各种数据项的一个数据库操作序列,这些操作要么全部执行,要么全部不执行,是一个不可分割的工作单位.事务由事务开始与事务结束之 ...