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.

InputThe 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.
OutputFor 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个不同的元素,
先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规定从小到大为标准次序),
于是在这n个元素的任一排列中,当某两个元素的先后次序与标准次序不同时,
就说有1个逆序。一个排列中所有逆序总数叫做这个排列的逆序数。
(以上文字引用自百度百科)
由于数据较大求逆序数就必须用复杂度较少的方法求,强行暴力求逆序数表示应该没人会选择这样做吧。
线段树就是一个非常好的数据结构用于求逆序数。
题意:一个有N个数的序列中,每次把第一个放在序列的最后一个形成新的序列,
求这些序列中最小的逆序数是多少?
求出原始序列的逆序数S,将a[i]移到最后一位时,新的序列的逆序数为
原来序列的逆序值S+比a[i]大的数的个数(n-1-a[i])-a[i](比a[i]小的个数)
如果这题本菜鸟有什么地方理解不正确,请读者在下方给我留言,纠正我的错误。
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 5010
int sum[maxn*+],a[maxn];
void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void build(int l,int r,int rt)
{
sum[rt]=;
if (l==r) return ;
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
}
void updata(int x,int l,int r,int rt )
{
if (l==r) {
sum[rt]++;
return ;
}
int m=(l+r)>>;
if (x<=m) updata(x,l,m,rt<<);
else updata(x,m+,r,rt<<|);
pushup(rt);
}
int query(int x,int y,int l,int r,int rt)
{
if (x<=l && r<=y) return sum[rt];
int m=(l+r)>>;
int ans=;
if (x<=m) ans+=query(x,y,l,m,rt<<);
if (y>m) ans+=query(x,y,m+,r,rt<<|);
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
build(,n-,);
int s=;
for (int i= ;i<n ;i++){
scanf("%d",&a[i]);
s+=query(a[i],n-,,n-,);
updata(a[i],,n-,);
}
int ans=s;
for (int i= ;i<n ;i++ ){
s+=(n-a[i]-)-a[i];
ans=min(ans,s);
}
printf("%d\n",ans);
}
return ;
}

Minimum Inversion Number~hdu 1394的更多相关文章

  1. 线段树 逆序对 Minimum Inversion Number HDU - 1394 Laptop

    Minimum Inversion Number HDU - 1394 求最小反转数,就是求最少的逆序对. 逆序对怎么求,就是先把所有的数都初始化为0,然后按照顺序放入数字,放入数字前查询从这个数往后 ...

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

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

  3. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

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

  4. hdu 1394 Minimum Inversion Number - 树状数组

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

  5. hdu 1394 Minimum Inversion Number 逆序数/树状数组

    Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...

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

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

  7. hdu 1394 Minimum Inversion Number(逆序数对) : 树状数组 O(nlogn)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394  //hdu 题目   Problem Description The inversion number ...

  8. HDU 1394——Minimum Inversion Number——————【线段树单点增减、区间求和】

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

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

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

随机推荐

  1. 3167: [Heoi2013]Sao [树形DP]

    3167: [Heoi2013]Sao 题意: n个点的"有向"树,求拓扑排序方案数 Welcome to Sword Art Online!!! 一开始想错了...没有考虑一个点 ...

  2. Does Java pass by reference or pass by value?(Java是值传递还是引用传递) - 总结

    这个话题一直是Java程序员的一个热议话题,争论不断,但是不论是你百度搜也好还是去看官方的文档中所标明的也好,得到的都只有一个结论:Java只有值传递. 在这里就不贴代码细致解释了,让我们来看看一些论 ...

  3. HttpGet HttpPost

    public string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)Web ...

  4. [Python Study Notes]cpu信息

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  5. 导入sass文件

    4导入sass文件 sass的@import规则在生成css文件时就把相关文件导入进来.这意味着所有相关的样式被归纳到了同一个css文件中,而无需发起额外的下载请求. 1 sass局部文件的文件名以下 ...

  6. 使用PowerDesigner对NAME和COMMENT互相转换

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 在使用PowerDesigner对数据库进行概念模型和物理模型设计时 ...

  7. 一个web应用的诞生(1)--初识flask

    基于flask的web应用的诞生 Flask是一个非常优秀的web框架,它最大的特点就是保持一个简单而易于扩展的小核心,其他的都有用户自己掌握,并且方便替换,甚至,你可以在社区看到众多开源的,可直接用 ...

  8. PHP 个人用到的琐碎代码记录

    查找字符串出现次数的方法 substr_count(string,substring,[start],[length]) 函数延迟代码执行若干秒,若成功,返回 0,否则返回 false. sleep( ...

  9. javascript 函数详解

    一.函数的一些基础概念: 1.js中的函数使用function来声明. 2.关于return: 2.1  函数在执行到return语句后悔立即停止并退出,return后面的代码永远不会得到执行: 2. ...

  10. 2018-03-03-解决win下凭据删除不干净而无法登录共项目录的问题

    layout: post title: 2018-03-03-解决win下凭据删除不干净而无法登录共项目录的问题 key: 20180303 tags: GIT 版本管理 modify_date: 2 ...