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. Linux下Apache重启遇到No space left on device错误的解决方法

      解决办法:1.输入:ipcs -s 看有没有超过5个,如果有请执行下面2的命令:2.ipcs -s | perl -ane '/^0x00000000/ && `ipcrm -s ...

  2. hdu 1524 A Chess Game 博弈

    题目链接 给出一个有向无环图, 上面放有一些棋子, 两个人轮流移动棋子, 当一个人无法移动时, 算输. 求出每一个点的sg值, 异或就可以.出度为0的点sg值为0. #include<bits/ ...

  3. Angular form

    参考 http://blog.xebia.com/2013/10/15/angularjs-validating-radio-buttons/ http://stackoverflow.com/que ...

  4. [问题解决] File "/struts-tags" not found

    错误:org.apache.jasper.JasperException: File "/struts-tags" not found 发生场景:tomcat服务器 解决方案:将t ...

  5. 射频识别技术漫谈(29)——射频接口芯片TRF7960

    TRF7960系列是TI推出的载波为13.56MHz.支持ISO15693.ISO14443A/B和FeliCa协议的射频接口芯片.许多人更关心它能不能读写MF1卡片,就我的理解及实际验证,由于MF1 ...

  6. 直接调用类成员函数地址(用汇编取类成员函数的地址,各VS版本还有所不同)

    在C++中,成员函数的指针是个比较特殊的东西.对普通的函数指针来说,可以视为一个地址,在需要的时候可以任意转换并直接调用.但对成员函数来说,常规类型转换是通不过编译的,调用的时候也必须采用特殊的语法. ...

  7. android数据库持久化框架

    android数据库持久化框架

  8. crtmpserver初探

    前言       Adobe的FMS(Flash Media Server)是很好用.但对应着分级授权的是money和有限功能开放.商业的东西既然用不起,也阻碍了我们的技术进步,那就只能求助于开源社区 ...

  9. To the Max(矩阵压缩)

    To the Max Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Su ...

  10. superMap Object 属性查看的一点代码

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...