Minimum Inversion Number

                                                                          Time Limit: 2000/1000 MS (Java/Others)    Memory Limit:
65536/32768 K (Java/Others)

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
 
Sample Output
16
 
题意:给出一个含有n个数的序列,然后每次都把第一个数放到序列的最后面形成新的序列,求这n个序列中最小的逆序数是多少?

分析:由于数列中每一个元素的值都不同样,所以我们仅仅需求出原始序列的逆序数,然后依据这个逆序数递推出其它序列的逆序数。
比如序列 2 0 3 1 4 5 的逆序数是3。把2放到最后边以后。比2小的数(2个)的每一个数的逆序数减1,比2大的数(n-a[i]-1个)的逆序数不变。而2的逆序数变为比2大的数的个数(n-a[i]-1)。依据这个结论,我们就能够递推出其它序列的逆序数,进而求出最小值。
求原始序列的逆序数时用线段树来求,对于第i个数,它的逆序数等于已经插入到线段树中的比它大的数的个数。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N = 5005;
#define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
int sum[N<<2], a[N]; void Push_Up(int root)
{
sum[root] = sum[root<<1] + sum[root<<1|1];
}
void build_tree(int l, int r, int root)
{
sum[root] = 0;
if(l == r) return;
int mid = (l + r) >> 1;
build_tree(lson);
build_tree(rson);
}
void update(int p, int l, int r, int root)
{
if(l == r)
{
sum[root]++;
return;
}
int mid = (l + r) >> 1;
if(p <= mid) update(p, lson);
else update(p, rson);
Push_Up(root);
}
int Query(int L, int R, int l, int r, int root)
{
if(L <= l && r <= R)
{
return sum[root];
}
int mid = (l + r) >> 1;
int ans = 0;
if(L <= mid) ans += Query(L, R, lson);
if(R > mid) ans += Query(L, R, rson);
return ans;
} int main()
{
int n, i;
while(~scanf("%d",&n))
{
build_tree(0, n-1, 1);
int res = 0;
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
res += Query(a[i], n-1, 0, n-1, 1); //查询已经插入到线段树中的数有多少个数比a[i]大
update(a[i], 0, n-1, 1); //把这个数插入到线段树中
}
int ans = res;
for(i = 0; i < n; i++)
{
res += (n - a[i] - 1) - a[i];
ans = min(ans, res);
}
printf("%d\n", ans); }
return 0;
}


hdu 1394 Minimum Inversion Number(线段树之 单点更新求逆序数)的更多相关文章

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

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

  2. hdu - 1394 Minimum Inversion Number(线段树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...

  3. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

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

  4. HDU 1394 Minimum Inversion Number 线段树

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...

  5. HDU 1394 Minimum Inversion Number(线段树 或 树状数组)

    题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...

  6. HDU 1394 Minimum Inversion Number (树状数组)

    题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...

  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 Minimum Inversion Number(树状数组/归并排序实现

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

  9. hdu 1394 Minimum Inversion Number(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...

随机推荐

  1. meminfo,df,

    yx100:root@yxRouter:/# cat /proc/meminfoMemTotal:         126584 kBMemFree:          103156 kBBuffer ...

  2. HttpClient使用详解

    http://itindex.net/detail/52566-httpclient HttpClient使用详解 标签: httpclient | 发表时间:2015-01-22 12:07 | 作 ...

  3. C#写的客户端连接 php的服务器端的小例子

    C#写的客户端连接 php的服务器端的小例子 php的server 端 <?php // server.php set_time_limit( 0 ); ob_implicit_flush(); ...

  4. C语言之基本算法35—数组上三角之积 主对角之积 副对角之积

    //数组算法 /* ============================================================= 题目:求四阶矩阵上三角之积.主对角之积,副对角之积: 如 ...

  5. Ubuntu下is not in the sudoers file 问题解决

    在Ubuntu12.04 下,使用sudo apt-get install XXX 时,突然跳出 username is not in the sudoers file的问题 然后我一查此userna ...

  6. MariaDb数据库管理系统学习(二)使用HeidiSQL数据库图形化界面管理工具

    HeidiSQL 是一款用于简单化的 MySQL server和数据库管理的图形化界面.该软件同意你浏览你的数据库,管理表,浏览和编辑记录,管理用户权限等等.此外,你能够从文本文件导入数据,执行 SQ ...

  7. poj 3450 Corporate Identity

    题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...

  8. Android 吸入动画效果详解

    1,背景 吸入(Inhale)效果,最初我是在iOS上面看到的,它是在Note程序中,用户可能添加了一页记录,在做删除时,它的删除效果是:这一页内容吸入到一个垃圾框的图标里面.请看下图所示: ==== ...

  9. STL 源代码分析 算法 stl_algo.h -- includes

    本文senlie原,转载请保留此地址:http://blog.csdn.net/zhengsenlie includes(应用于有序区间) ------------------------------ ...

  10. PVPlayer的实现方式

    关于opencore下多媒体播放,在mediaserver进程里面仅仅有一行代码: MediaPlayerService::instantiate(); 这行代码的作用是初始化一个MediaPlaye ...