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. webpack前端模块加载工具

    最近在看许多React的资料,发现了大部分的项目都是用webpack行模块化管理的工具.这次也是借着写了一个React-Todos的小应用,对webPack最基本实用的功能体验了一番,顺带做个小记录. ...

  2. 移去OleContainer的黑边框

    //禁止双击打开word编辑 olecontainer1.AutoActivate := aaManual; //禁止右键菜单 olecontainer1.AutoVerbMenu := False; ...

  3. c++实现扫雷(坐标)

    昨天在观察贪食蛇的代码时,看到了有如何实现扫雷的c++代码,觉得挺有趣,今天便又试了一下 #include <ctime> #include <cstdlib> #includ ...

  4. IDEA kotlin 配置

    修改 idea 安装目录 bin 目录 下 idea.properties   文件修改idea.max.intellisense.filesize=50000 避免proto 生成的java文件不被 ...

  5. groovy 环境配置

    win7: 下载 http://www.groovy-lang.org/download.html 将解压后的文件夹里的bin目录加入path环境变量

  6. PR合并回写

    ) as LGORT ,'SAPRFC' as ERNAM,out_pr.due_datetime,out_pr.so_id,out_pr.so_lineid,out_pr.sobsl from V_ ...

  7. Matlab中插值函数汇总(上)

    Matlab中插值函数汇总分上下两个部分,主要整合自matlabsky论坛dynamic发表于2009-2-21 21:53:26 的主题帖,以及豆丁网rickoon上传的教材第8章<插值,拟合 ...

  8. swift 移除所有子控件

    /// 移除所有子控件 func removeAllSubViews(){ if self.view.subviews.count>0{ self.view.subviews.forEach({ ...

  9. 如何修改jenkins的启动用户?

    如何修改运行jenkins进程的linux帐号? 1.找的jenkins的配置文件,一般是/etc/sysconfig/jenkins 2.修改下面的参数为相应的用户,比如JENKINS_USER=& ...

  10. linux下nginx tomcat集群

    集群系统一般通过两台或多台节点服务器系统通过相应的硬件及软件互连,每个群集节点都是运行其自己进程的独立服务器. 这些进程可以彼此通信,对网络客户机来说就像是形成了一个单一系统,协同起来向用户提供应用程 ...