http://acm.hdu.edu.cn/showproblem.php?pid=1394

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8205    Accepted Submission(s): 5041

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
#include<stdio.h>
#define maxn 500004
struct node
{
int left,right;
int sum;
};
int val[maxn];
int n;
node tree[*maxn];
void build(int left,int right,int i)
{
tree[i].left =left;
tree[i].right =right;
tree[i].sum =;
if(tree[i].left ==tree[i].right )
return ;
build(left,(left+right)/,*i);
build((left+right)/+,right,*i+);
}
void update(int r,int j)
{
tree[r].sum++;
if(tree[r].left==tree[r].right)
return ;
int mid=(tree[r].left +tree[r].right )/;
if(j<=mid)
update(r*,j);
if(j>mid)
update(r*+,j);
}
int getsum(int p, int left, int right)
{
if (left > right)
return ;
if (tree[p].left == left && tree[p].right == right)
return tree[p].sum;
int m = (tree[p].left + tree[p].right) / ;
if (right <= m)
return getsum(p*, left, right);
else if (left > m)
return getsum(p*+, left, right);
else return getsum(p*, left, m) + getsum(p*+, m + , right);
}
int main()
{
while (~scanf("%d",&n))
{
build(,n - ,);
int i,sum =,ans;
for (i = ;i<= n; i++)
{
scanf("%d", &val[i]);
sum += getsum(, val[i], n - );
update(, val[i]);
}
ans = sum;
for (i = ; i <= n; i++)
{
sum = sum + (n - val[i] - ) - val[i];
ans = sum < ans ? sum : ans;
}
printf("%d\n", ans);
}
return ;
}
 

HDU1394-Minimum Inversion Number的更多相关文章

  1. hdu1394(Minimum Inversion Number)线段树

    明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...

  2. [hdu1394]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 线段树+逆序对

    仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...

  4. HDU1394 Minimum Inversion Number(线段树OR归并排序)

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

  5. HDU-1394 Minimum Inversion Number(线段树求逆序数)

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

  6. 2018.07.08 hdu1394 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/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/O ...

  8. HDU-1394 Minimum Inversion Number (逆序数,线段树或树状数组)

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

  9. hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)

    题目传送门 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  10. 【线段树】HDU1394 - Minimum Inversion Number

    [题目大意] 给出0..n-1组成的一段数,可以移动前几个数到结尾.求出最小的逆序对个数. [思路] 先用线段树求出逆序对,方法和树状数组是一样的.然后对于当前第一个数num[0],在它之后比它小的数 ...

随机推荐

  1. css样式 第6节

    程序员语录: 不要太刻意地把写程序这件事和挣钱挂起来,局限了你挣钱的本事 <html> <head> <title>网页样式</title> </ ...

  2. Spring配置,JDBC数据源及事务

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. String类与Date类的转换

    public class DateTest { public static void main(String[] args) throws ParseException { Date date = n ...

  4. "ERR_GFX_D3D_INIT", GTA5-报错解决办法

    GTA5 PC 版,online模式报错“ERR_GFX_D3D_INIT”. 网上搜到一篇文章,可以解决此问题: http://fixcrasheserrorguide.com/fix-grand- ...

  5. MVC的发展

    ASP.NET下的MVC从原始的1.0走到2.0,再到3.0,现在走到4.0,也许明年5.0就问世了,先不管那些,那说说这些MVC在ASP.NET是如何变化发展的.对于.net编程人员来说可能会很熟悉 ...

  6. asmdisk 丢失问题一次记录

    环境 vm12 workstation ,11.2R 在安装RAC 第二台机器不显示磁盘的是问题 , oracleasm listdisks 查询没有结果 , 于是执行 oracleasm scand ...

  7. SSH框架jar神包

    SSH JAR包链接(https://zhidao.baidu.com/share/ac8b1389bac84023d7442cad88f3933c.html)

  8. (jQuery 插件)封装容器的表单为json对象

    下面代码可以把一个页面容器中的表单元素封装成一个json对象. (function($){ $.fn.serializeObject=function(){ var inputs=$(this).fi ...

  9. centos 软件安装 删除

    centos的软件安装大致可以分为两种类型: [centos]rpm文件安装,使用rpm指令  类似[ubuntu]deb文件安装,使用dpkg指令 [centos]yum安装   类似[ubuntu ...

  10. Delphi接口

    program Demo1; { Create Date: 2014-06-29 Author: P.S.M 1.接口Demo1 } {$APPTYPE CONSOLE} uses SysUtils; ...