B - Minimum Inversion Number

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

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

本题的意思是求逆序数,可以将前m个数放到序列最后面,求逆序数的最小值
方法就是找出每个数之前有多少个比它大的数累加即可,处理时可以用树状数组,也可以用线段树,我用的是线段树
先建一棵空树,依次处理每个元素,把线段树的对应节点a[i]++,然后在原序列中,在a[i]前面且比它大的就是在线段树中
a[i+1]到n这段区间和了;
这样容易找原序列的逆序数,至于调动是有个规律的,每次把第一个移到后面,逆序数就等于原来的-(后面比a小的)+(比a大的)




代码如下




#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f int a[5005];
int tree[5005*4]; void build(int n,int l,int r)
{
if(l==r)
{
tree[n]=0;
return;
}
int m=(l+r)>>1;
build(n<<1,l,m);
build(n<<1|1,m+1,r);
tree[n]=tree[n<<1]+tree[n<<1|1];
} void update(int n,int l,int r,int pos)
{
if(l==r)
{
tree[n]++;
return;
}
int m=(l+r)>>1;
if(pos<=m)
update(n<<1,l,m,pos);
else
update(n<<1|1,m+1,r,pos);
tree[n]=tree[n<<1]+tree[n<<1|1];
} int query(int n,int l,int r,int ll,int rr)
{
if(ll<=l&&rr>=r)
{
return tree[n];
} int ans=0;
int m=(l+r)>>1;
if(ll<=m)
ans+=query(n<<1,l,m,ll,rr);
if(rr>m)
ans+=query(n<<1|1,m+1,r,ll,rr);
return ans;
} int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
a[i]++;
}
build(1,1,n);
int cnt=0;
for(int i=1; i<=n; i++)
{
update(1,1,n,a[i]);
cnt+=query(1,1,n,a[i]+1,n);
} int mx=cnt;
for(int i=1; i<=n; i++)
{
cnt=cnt-(a[i]-1)+(n-a[i]);
mx=min(mx,cnt);
}
printf("%d\n",mx);
}
return 0;
}





HDU1349 Minimum Inversion Number 2016-09-15 13:04 75人阅读 评论(0) 收藏的更多相关文章

  1. js笔记 标签: javascript 2016-08-01 13:30 75人阅读 评论(0) 收藏

    typeof可以用来检测给定变量的数据类型,typeof是一个操作符而不是函数,所以圆括号可以省略. Undefined类型只有一个值,即特殊的undefined.在使用var声明变量但未对其加以初始 ...

  2. Segment Tree 分类: ACM TYPE 2014-08-29 13:04 97人阅读 评论(0) 收藏

    #include<iostream> #include<cstdio> using namespace std; struct node { int l, r, m; int ...

  3. Gold Coins 分类: POJ 2015-06-10 15:04 16人阅读 评论(0) 收藏

    Gold Coins Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21767   Accepted: 13641 Desc ...

  4. Codeforces805D. Minimum number of steps 2017-05-05 08:46 240人阅读 评论(0) 收藏

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

  5. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

  6. Power Network 分类: POJ 2015-07-29 13:55 3人阅读 评论(0) 收藏

    Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24867 Accepted: 12958 Descr ...

  7. HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏

    Red and Black Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  8. Codeforces 766C Mahmoud and a Message 2017-02-21 13:57 62人阅读 评论(0) 收藏

    C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. Eclipse 快捷键大全 分类: C_OHTERS 2014-06-01 13:05 332人阅读 评论(0) 收藏

      精选常用: 1.  ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如a ...

随机推荐

  1. spring 3.x + hibernate4.x 实现数据延迟加载

      Spring为我们解决Hibernate的Session的关闭与开启问题. Hibernate 允许对关联对象.属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Sess ...

  2. python的可变list和不可变tuple, dict和set

    list和tuple 在python中分为可变表和不可变表: 类型 名称 表示方法 可变 list [] 不可变 tuple () list list是可变表,list内部索引从0开始,正整数是正序的 ...

  3. zabbix出现中文不能选的情况

    像这里一样,有些选项是选不了的,这个时候我们要做的第一步就是,找到这个配置文件. 如果不知道在哪里的话可以用find命令查找. sudo find / -name locales.inc.php 找到 ...

  4. 关于number...的精度问题

    一 当数字的精度被定为number(3,2)时, 这时他能输入的数字整数部分只能是3-2=1位, 小数位如果不够会用0补齐, 超出的四舍五入保留3位小数. SQL> insert into t_ ...

  5. Windows phone自定义控件(无外观控件)——FlipPanel

    编码前 无外观自定义控件的定义在上一篇中已经有了,至于这一篇的自定义控件,比之前多加入了状态的变化,就像默认的Button具有Pressed.Normal等状态.在状态转变的同时可以加上一些动画,可以 ...

  6. CSS文字大小单位px、em、pt详解

    这里引用的是Jorux的“95%的中国网站需要重写CSS”的文章,题目有点吓人,但是确实是现在国内网页制作方面的一些缺陷.我一直也搞不清楚px与em之间的关系和特点,看过以后确实收获很大.平时都是用p ...

  7. stuff函数(转)

    在上篇博文中提到了stuff函数 在这篇博文中对stuff函数进行了详解 本片博文系转载,但对原文顺序做了下调整 示例 以下示例在第一个字符串 abcdef 中删除从第 2 个位置(字符 b)开始的三 ...

  8. 机械硬盘怎么看是否4k对齐

    在XP.VISTA.win7系统下,点击“开始”,“运行”,输入“MSINFO32”,点击“确定”,出现如下显示的界面,依次点击“组件/存储/磁盘”,查看“分区起始偏移”的数值,如果不能被4096整除 ...

  9. 对.NET中导出数据到EXCEL的几种方法探讨

    最近在做一个报表系统的时候,需要把DATASET中的数据导到EXCEL当中,于是在网上找了一遍,发现了好几种方法,本来以为应该差不多,但后来经过一一试用后,发现在性能上真的差别很大,现在就介绍一下,同 ...

  10. swift VFL - 父视图是scrollview 注意点

    1. scrollview 添加 子控件, 必须设置 宽度和高度, 他自己无法撑满2. scrollview的子空间的约束  只能相对于 左边 的顶部, 无法设置右边和底部的约束 3. 设置最小距离 ...