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种序列中倒置数的最小个数,即满足下标i<j,a[i]>a[j]。

其实说白了就是求不停倒置的最小逆序数

求逆序数用线段树或者树状数组都是挺方便的,每次加一个数进去,看此时后面的数有几个。

但这样肯定会超时,我们可以首先写出一开始的逆序数,发现每次倒置后,逆序数为减少a[i],而增加n-1-a[i]的。减少比它小的,增加比它大的

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int a[maxn<<];
int add[maxn];
#define lson l,m,k<<1
#define rson m+1,r,k<<1|1
void hehe(int k)
{
a[k]=a[k<<]+a[k<<|];
}
void build(int l,int r,int k)
{
a[k]=;//先是假设这棵树都为0
if(l==r) return ;
int m=(l+r)>>;
build(lson);
build(rson);
}
int query(int x,int y,int l,int r,int k)
{
if(x<=l&&r<=y)
return a[k]; int m=(l+r)>>;
int s=;
if(x<=m) s+=query(x,y,lson);
if(y>m) s+=query(x,y,rson);
return s;
}
void update(int p,int l,int r,int k)
{
if(l==r)
{
a[k]++;//增加个数
return;
}
int m=(l+r)>>;
if(p<=m) update(p,lson);
else update(p,rson);
hehe(k);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
build(,n-,);
int sum=;//为一开始的逆序数
for(int i=;i<n;i++)
{
scanf("%d",&add[i]);
sum+=query(add[i],n-,,n-,);
update(add[i],,n-,);
}
int minn=sum;
for(int i=;i<n;i++)
{
sum+=n-add[i]-add[i]-;
minn=min(minn,sum);
}
cout<<minn<<endl;
}
return ;
}

HDU 1394Minimum Inversion Number的更多相关文章

  1. HDU 1394Minimum Inversion Number 数状数组 逆序对数量和

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

  2. HDU 1394Minimum Inversion Number(线段树)

    题目大意是说给你一个数组(N个),没戏可以将其首部的k(k<N)个元素移动至尾部,这样总共会形成N个序列 现在要求这n个序列中逆序对数最少的那一个序列有多少个逆序对 最初的确是没太多思路,就算知 ...

  3. HDU 1394-Minimum Inversion Number(BIT)

    题意: 给你n个数字的序列 每次把第一个数字放到最后 得到一个新序列 一共有n个序列求这些序列中哪个序列含最小的总的逆序数 (输出最小总逆序数) 分析: 用BIT求出初始各数的逆序数,第一个数放最后它 ...

  4. 1394-Minimum Inversion Number

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

  5. hdu Minimum Inversion Number(逆序数的小知识与线段树)

    飞! 题解 首先,求逆序数对的思路: 1.得到整个数列后,从前往后扫,统计比a[i]小的,在a[i]后面的有多少个 这样做的话,应该是只有n2的暴力作法,没想到更好的方法 2.统计a[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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  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 逆序数/树状数组

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

随机推荐

  1. 关于sharepoint如何做SSO,如何做OOS监视编辑

    应客户需求,需要做sharepoint SSO,以前都是默认的AD验证,如果客户已经有一套SSO系统,验证过SSO之后就能自动登录,而不是浏览器上设置保存用户名密码的AD登陆. 怎么做呢? 首先sha ...

  2. 【JavaScript-基础-cookie从入门到进阶】

    cookie 关于cookie 用于方便服务端管理客户端状态提出的一种机制. document.cookie 客户端JavaScript可通过document.cookie方式获取非HTTPOnly状 ...

  3. Python 学习笔记(十二)Python文件和迭代(一)

    文件 文件和文件夹 文件:文本文件.二进制文件 文件夹:(windows) G:\pythonWorkspace\python\study (linux/mac) /home/workspace/py ...

  4. Python 学习笔记(十一)Python语句(一)

    运算符和条件语句 算术运算符 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 - 两个数相乘 ...

  5. java中exception和error有什么区别,运行时异常和一般异常有什么区别

    1.exception和error都是继承了throwable类,在java中只有throwable类型的实例才可以被抛出(throw)或者捕获(catch),它是异常处理机制的基本组成类型 2.ex ...

  6. dubbox provider cosumer 包坐标及xml

    provider pom <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...

  7. 慎使用sql的enum字段类型

    在sql的优化中,会有同学提到一点:使用enum字段类型,代替其他tinyint等类型.以前这也是不少人喜欢优化的,但是现在细想,是非常不合理的. 优点: 1.可以设置区间范围,比如设置性别:1男2女 ...

  8. restframework中的那些参数你知道吗?

    序列化是很重要的过程, 在构建数据结构的时候, 往往会出现很多意想不到的问题, 有一些参数你要用, 但是没有办法穿过来, 怎么办> 今天这篇博客就是写我之前的一个小项目中用restframewo ...

  9. Win7配置express4环境

    本机环境: $ node -v v10.14.1 $ npm -v 6.4.1 配置node环境变量: #配置全局安装目录 npm config set prefix "G:\WEB\nod ...

  10. Java核心技术36讲----------谈谈final、finally、finalize有什么不同

    一.final 1.final修饰方法时,需要注意的点: #final修饰方法时,之前的第二个原因是效率.但是如果方法过于庞大,可能看不到内嵌调用带来的任何性能提升.在最近的Java版本中,不需要使用 ...