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. map标签的详细使用参数

    map标签必须成对出现,即 <map> ....</map> 同时map必须和area配合使用. img标签里的usermap属性值必须与map标签里的id和name值完全一致 ...

  2. 14.6.2 Moving or Copying InnoDB Tables to Another Machine 移动或者copy InnoDB 表到另外的机器

    14.6.2 Moving or Copying InnoDB Tables to Another Machine 移动或者copy InnoDB 表到另外的机器 这个章节描述技术关于移动或者copy ...

  3. php 和thinkphp 对excel操作

    php对excel的操作主要通过引入 excel_reader2.php 或者是PHPExcel 类进行   两个文件自行下载 php 对其读操作: 文件目录结构 excel_reader2.php ...

  4. 基于飞思卡尔i.MX 6Quad Sabrelite开发板的触摸屏调试

    1      概述 本次任务是在飞思卡尔i.MX 6Quqd Sabrelite开发板上调试触屏驱动,触屏芯片是Goodix的gt828芯片,触屏接口是I2C. 操作系统:android 4.0.4 ...

  5. Window平台搭建Redis分布式缓存集群 (一)server搭建及性能測试

    百度定义:Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对很多其它.包含string(字符串).list(链表).set(集合).zset(sort ...

  6. Github干货系列:C++资源集合-

    Awesome CPP,这又是一个 Awesome XXX 系列的资源整理,由 fffaraz 发起和维护.内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. ...

  7. 冒泡排序 JAVA版

    冒泡排序 算法思想是每次从数组末端开始比较相邻俩元素,把第i小的冒泡到数组的第i个位置.i从0一直到N-1从而完成排序.当然也可以从数组开始端开始比较相邻两元素,把第i大的冒泡到第N-i个位置.I从0 ...

  8. JavaScript2谁刚开始学习应该知道4最佳实践文章(翻译)

    原版的:24 JavaScript Best Practices for Beginners (注:阅读原文的时候没有注意公布日期,觉得不错就翻译了,翻译到JSON.parse那一节觉得有点不正确路才 ...

  9. Nagios的客户端的安装

    一.Linux服务器的nagios客户端的安装 步骤: 1.  创建目录,上传文件到该目录 mkdir /data nagios-plugins-2.1.1.tar.gz nrpe-2.12.tar. ...

  10. CC 3-Palindromes(manacher)

    传送门:3-Palindromes 题意:求为回文串且能整除3且不前导0的子串个数. 分析:由 manacher算法O(N)可算出以i为坐标的最长为p[i]回文子串,且Si-k,Si-k+1..... ...