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]

#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;

HDU-1394 Minimum Inversion Number(线段树求逆序数)的更多相关文章

  1. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

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

  2. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  3. hdu - 1394 Minimum Inversion Number(线段树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...

  4. HDU 1394 Minimum Inversion Number 线段树

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...

  5. HDU 1394 Minimum Inversion Number(线段树 或 树状数组)

    题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...

  6. HDU_1394_Minimum Inversion Number_线段树求逆序数

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

  7. hdu 13394 Minimum Inversion Number 线段树

    题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...

  8. hdu 1394 Minimum Inversion Number (树状数组求逆序对)

    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...

  9. HDU 1394 Minimum Inversion Number (树状数组)

    题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...

  10. HDU 1394 Minimum Inversion Number(树状数组/归并排序实现

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

随机推荐

  1. 用highcharts展现你的数据

    摘要: 前面已经分享过图表插件,今天在来将下如何使用highcharts来绘制图表.highcharts支持在线定制,你可以选择你所需要的模块,然后点击build就会生成一个js文件链接,右键保存到本 ...

  2. 一道简单的把ArrayList中的正负数组分开并求得边界索引的题目

    给定一个List,里面存放的一组整数有正数和负数,要求把正数和负数分开,并得到正数和负数分割线索引(不要求排序,不能使用多层循环) 解答方法并不算太复杂,重点注意边界条件和极端条件(全是正或者全是负) ...

  3. 6.824 Lab 5: Caching Extents

    Introduction In this lab you will modify YFS to cache extents, reducing the load on the extent serve ...

  4. zabbix添加对tomcat线程池的监控

    在zabbix模板中添加以下监控项: 可以参考文档:http://www.fblinux.com/?p=616

  5. An internal error occurred during: "Launching xxx on WebLogic10.x".

    An internal error occurred during: "Launching xxx on WebLogic10.x". java.lang.NullPointerE ...

  6. 递归的几个demo

    /** * Created by root * Description : 递归函数 */ object RecursionTest { def main(args: Array[String]): ...

  7. WebForm的初步认识

    嘿嘿,这里就简单的总结一下初步学习webform以及对他的认识,其实大家都认为webform很讨厌,因为好多都是给我们封装好的,而且现在好多的公司已经慢慢的从中逃离出来选择使用mvc架构,甚至好多的项 ...

  8. react中的hoc和修饰器@connect结合使用

    在学习react-redux的时候,看到了修饰器这个新的属性,这个是es7的提案属性,很方便.于是我用@connect代替了connect(使用的时候需要配置,这里不赘述),省去了很多不必要的代码,但 ...

  9. sql语句建表,并且自增加主键

    sql语句建表,并且自增加主键 use [test] CREATE TABLE [dbo].[Table_4] ( [userid] [int] IDENTITY(1,1) NOT NULL, CON ...

  10. Android Studio中R报错(cnanot resolve symbol R)

    我的解决办法: Tools -> Android -> Sync Project with Gradle Files Build -> Clean Project 然后就好了 PS: ...