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. Mvc--Html.ActionLink()用法

    },new{ target="_blank"})会生成 <a href="Products/Detail/1" target="_blank&q ...

  2. wcf综合运用之:大文件异步断点续传

    在WCF下作大文件的上传,首先想到使用的就是Stream,这也是微软推荐的使用方式.处理流程是:首先把文件加载到内存中,加载完毕后传递数据.这种处理方式对小文件,值得推荐,比如几K,几十k的图片文件, ...

  3. hive支持sql大全

    转自:http://www.aboutyun.com/thread-7316-1-1.html 一.关系运算:1. 等值比较: = 语法:A=B 操作类型:所有基本类型 描述: 如果表达式A与表达式B ...

  4. linux杂记(六)档案权限

    [KANO@kelvin ~]$ ls -al 总用量 drwx------. KANO KANO 10月 : . drwxr-xr-x. root root 9月 : .. drwxrwxr-x. ...

  5. classpath的总结

    转自:http://blog.csdn.net/javaloveiphone/article/details/51994268   版权声明:本文为博主原创文章,未经博主允许不得转载. 1.src不是 ...

  6. Android 仿360桌面小人

    首先自定义FloatsWindowView,用于显示动画小人. import android.annotation.SuppressLint; import android.content.Conte ...

  7. grunt打包过程中的注意点

    1.安装nodeJS   nodeJS下载地址: http://www.nodejs.org/download/ 2.   在Node.js command prompt 这个控制面板输入 npm i ...

  8. mysql 性别存储

    大家在设计数据库时,碰到 性别.状态等 这些 值比较固定的列时,数据类型 是如何定义? 通常都是采用 : 1 create table `XXX` 2 ( 3 ........ 4 sex int(1 ...

  9. Javascript 风格向导

    序   大部分针对Javascript最合理的方法归纳.   类型   • 原始类型:我们可以直接使用值. ο string ο number ο boolean ο null ο undefined ...

  10. Mysql 学习记录

    ( xampp 的mysql 与 直接用 dnf 安装的 mysql 有冲突! ) 1. 数据库基本知识: 一张表的行 又称为 记录 一张表的列 又称为 字段 表结构:所有字段,规定了你的每一条记录所 ...