Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15517    Accepted Submission(s): 9467

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

mergesort

 
Sample Output
16
 
Author
CHEN, Gaoli

题解:线段树;也可以用归并排序,也可以用树状数组;注意每次把第一个放在最后这个条件;归并:http://www.cnblogs.com/handsomecui/p/4814442.html

mergesort

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
const int MAXN=5010;
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define pushup tree[root]=tree[root<<1]+tree[root<<1|1]
#define mem(x,y) memset(x,y,sizeof(x))
int tree[MAXN<<2];
int a[MAXN];
int ans;
void update(int v,int root,int l,int r){
int mid=(l+r)>>1;
if(l==v&&r==v){
tree[root]++;
return;
}
if(mid>=v)update(v,lson);
if(mid<v)update(v,rson);
pushup;
}
void query(int L,int R,int root,int l,int r){
int mid=(l+r)>>1;
if(l>=L&&r<=R){
ans+=tree[root];
return;
}
if(mid>=L)query(L,R,lson);
if(mid<R)query(L,R,rson);
}
int main(){
int N;
while(~scanf("%d",&N)){
mem(tree,0);
ans=0;
int x;
for(int i=0;i<N;i++){
scanf("%d",&x);a[i]=x;
query(x+1,N-1,1,0,N-1);
// printf("%d\n",ans);
update(x,1,0,N-1);
}
int cnt=ans;
for(int i=0;i<N;i++){
cnt=cnt+N-1-a[i]-a[i];
ans=min(ans,cnt);
}
printf("%d\n",ans);
}
return 0;
}

  

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_线段树求逆序数

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

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

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

  4. hdu 13394 Minimum Inversion Number 线段树

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

  5. HDU - 1394 Minimum Inversion Number (线段树求逆序数)

    Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs ( ...

  6. HDU-1394 Minimum Inversion Number(线段树求逆序数)

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

  7. hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)

    题目传送门 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  8. hdu1394--Minimum Inversion Number(线段树求逆序数,纯为练习)

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

  9. <Sicily>Inversion Number(线段树求逆序数)

    一.题目描述 There is a permutation P with n integers from 1 to n. You have to calculate its inversion num ...

随机推荐

  1. 小鱼提问1 类中嵌套public修饰的枚举,外部访问的时候却只能Class.Enum这样访问,这是为何?

    /// <summary> /// 常量等定义 /// </summary> public class General { /// <summary> /// 文件 ...

  2. 创建一个ROS包

    先前笔者不知道catkin到底是个什么东东,后来终于在官方网站上找到了答案,原来catkin是ROS的一个官方的编译构建系统,是原本的ROS的编译构建系统rosbuild的后继者.catkin的来源有 ...

  3. AXIS2远程调用WebService示例(Eclipse+AXIS)

    转自:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/10/3071584.html 我们将Web Service发布在Tomcat或者其他应 ...

  4. 一道Python练习题

    有关字符串与编码的,转自廖雪峰Python教程. 小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,并用字符串格式化显示出'xx.x%',只保留小数点后1位: # -*- co ...

  5. nyist 220 推桌子

    题目链接:推桌子 题目意思:给你一些操作,将S出的桌子推到L出,但是这个过道有时会被占用,推一次是10min,不影响的操作可以同时开始,并且只记一次. 思路:贪心,首先按照S从小到大排序,决策:从第一 ...

  6. linux内核升级图文攻略

    Linux内核概览 Linux是一个一体化内核(monolithic kernel)系统. 设备驱动程序可以完全访问硬件. Linux内的设备驱动程序可以方便地以模块化(modularize)的形式设 ...

  7. Win7下超级管理员创建普通权限任务

    已转至新的博客 http://www.raysoftware点击打开链接.cn/?p=49 项目中用到一个功能,Win7下超级管理员创建普通权限任务. 试了几种办法,例如获取资源管理器的Token,然 ...

  8. linux Bash bug修复记录

  9. mysql外键使用和级联

    如下面的: create table applicant (id int not null auto_increment primary key, jobId int not null, studen ...

  10. 害人的VS2008,manifest导致“应用程序配置不正确,应用程序未能启动”

    在VC++2008的项目中,如何显示地指定要使用的C++库的版本? 开发环境:VS2008 SP1 + Win2003 SP2         因为我的VS2008安装了SP1补丁,所以有了9.0.3 ...