Minimum Inversion Number  

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
题目大意就是给你 N 个区间,里面包含有0~N-1的整数,允许你每次将序列的头调到序列尾,让你求出最小的逆序和,
思路:先用线段树or树状数组预处理出原先的逆序和,然后再一个一个推出,每次操作后所产生的的新的逆序和,就拿样例来说,一开始的逆序和是22,如果将1调到末尾那里的话,原先在1后面比1小的数有1个,这个数是0,而当1调到后面的时候,在1前面的比一大的数有8个,为3,6,9,8,5,7,4,2,那么新序列的逆序和为22 - 1 + 8 = 29。以此类推
 
AC代码:线段树的方法,借鉴了notonlysuccess大神的姿势:
Memory: 320 KB   Time: 78 MS
#include <cstdio>
#include <iostream>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = ;
int sum[maxn<<];
void PushUp(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
}
void build(int l,int r,int rt){
sum[rt] = ;
if(l == r) return ;
int m = (l + r) >> ;
build(lson);
build(rson);
return ;
}
void update(int p,int l,int r,int rt){
if(l == r){
sum[rt]++;
return ;
}
int m = (l + r)>>;
if(p <= m) update(p,lson);
else update(p,rson);
PushUp(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L <= l&&r <= R){
return sum[rt];
}
int m = (l + r)>>;
int ret = ;
if(L <= m) ret += query(L,R,lson);
if(R > m) ret += query(L,R,rson);
return ret;
}
int x[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
build(,n - ,);
int sum = ;
for(int i = ;i < n;i++){
scanf("%d",&x[i]);
sum += query(x[i],n - ,,n - ,);
update(x[i],,n - ,);
}
int res = sum;
for(int i = ;i < n;i++){
sum += n - x[i] - x[i] - ;
res = min(res,sum);
}
printf("%d\n",res);
}
return ;
}

后来我有用树状数组做了一下,发觉树状数组一般都要比线段树要快~

Memory: 332 KB   Time: 31 MS
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = ;
int c[maxn];
int a[maxn];
int n;
inline int lowbit(int x){
return x&(-x);
}
int sum(int x){
int ret = ;
while(x > ){
ret += c[x];x -= lowbit(x);
}
return ret;
}
void add(int x,int d){
while(x <= n){
c[x] += d;x += lowbit(x);
}
}
int main(){
while(~scanf("%d",&n)){
memset(c,,sizeof(c));
memset(a,,sizeof(a));
int ans = ;
for(int i = ;i <= n;i++){
scanf("%d",&a[i]);
ans += i - - sum(a[i]);
add(a[i] + ,);
}
//cout<<ans<<endl;
int MIN = ans;
for(int i = ;i <= n;i++){
MIN += n - (a[i] + ) - (a[i]);
ans = min(MIN,ans);
}
printf("%d\n",ans);
}
return ;
}

HDU 1394 线段树or 树状数组~的更多相关文章

  1. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  2. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

  3. HDU 1166 敌兵布阵 (数状数组,或线段树)

    题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

  4. HDU 1394 Minimum Inversion Number (树状数组 && 规律 && 逆序数)

    题意 : 有一个n个数的数列且元素都是0~n-1,问你将数列的其中某一个数及其前面的数全部置到后面这种操作中(比如3 2 1 0中选择第二个数倒置就产生1 0 3 2)能产生的最少的逆序数对是多少? ...

  5. hdu 1394 线段树

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

  6. HDU 2492 Ping pong (数状数组)

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. hdu 1394 Minimum Inversion Number - 树状数组

    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...

  8. hdu 1394(线段树) 最小逆序数

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 给出一列数组,数组里的数都是从0到n-1的,在依次把第一个数放到最后一位的过程中求最小的逆序数 线段树的应 ...

  9. hdu 1394 线段树计算逆序数

    线段树计算逆序数的原理: 用线段树来统计已插入的数的个数(所以要保证最大的那个数不能太大,否则数组都开不了),然后每插入一个数,就查询比插入的数大的个数,累加即可. 这个题还有一个特点就是,题目给的是 ...

随机推荐

  1. assert.notStrictEqual()详解

    严格不相等测试,由不全等运算符确定(===). const assert = require('assert'); assert.notStrictEqual(1, 2); // OK assert. ...

  2. C#上位机开发(四)—— SerialAssistant功能完善

    上一篇中我们完成了一个串口助手的雏形,实现了基本发送和接收字符串功能,并将打开/关闭串口进行了异常处理,这篇就来按照流程,逐步将功能完善: 1.构思功能 首先是接收部分,要添加一个“清空接收”的按钮来 ...

  3. hive 删除表内容

    TRUNCATE:truncate用于删除所有的行,这个行为在hive元存储删除数据是不可逆的delect:用于删除特定行条件,你可以从给定表中删除所有的行insert overwrite table ...

  4. BNUOJ 2105 Distance Queries

    Distance Queries Time Limit: 2000ms Memory Limit: 30000KB This problem will be judged on PKU. Origin ...

  5. BNUOJ 6023 畅通工程续

    畅通工程续 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 1874 ...

  6. <<编程之美>> -- 队列中取最大值操作的问题

    不得不说编程之美是一本好书,虽然很多题目在做acm中的过程中遇到过,不过还是有很多值得思考的地方 这是今天在编程之美上看到的一个问题,对于栈转化成队列的一个思考 平时都太过依赖c++内函数库中的栈和队 ...

  7. android开发里跳过的坑——android studio升级完成后eclipse adt无法正常使用

    最近有时间,把android studio做了一次升级,升级完成后,悲催的发现eclipse不能正常运行了,网上查了好多资料,试了很多方法都不行,最后把eclipse使用的sdk与AS使用的SDK区分 ...

  8. Linux下tomcat的catalina.out屏蔽

    修改catalina.sh ,找到下面的位置: if [ -z "$CATALINA_OUT" ] ; then#CATALINA_OUT="$CATALINA_BASE ...

  9. 1、Java并发性和多线程-并发性和多线程介绍

    以下内容转自http://ifeve.com/java-concurrency-thread/: 在过去单CPU时代,单任务在一个时间点只能执行单一程序.之后发展到多任务阶段,计算机能在同一时间点并行 ...

  10. 淘宝后台添加颜色尺码动态sku

    废话不多说,直接上代码,用了vue,可直接copy运行 <!DOCTYPE html> <html lang="en"> <head> < ...