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
先求出逆序数,在一个个dp
 #include"iostream"
#include"cstdio"
#include"string"
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define MAX 5004
int s[MAX];
int sum;
struct node
{
int l;
int r;
int va;
}tree[MAX<<];
void build(int l,int r,int rt)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].va=;
if(tree[rt].l==tree[rt].r)
return ;
int mid=(tree[rt].l+tree[rt].r)>>;
build(lson);
build(rson);
}
void updata(int x,int rt)
{
if(tree[rt].l==tree[rt].r)
{
tree[rt].va=;
return;
}
int mid=(tree[rt].l+tree[rt].r)>>;
if(x<=mid)
updata(x,rt<<);
else
updata(x,rt<<|);
tree[rt].va=tree[rt<<].va+tree[rt<<|].va;
}
void query(int l,int r,int rt)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
sum+=tree[rt].va;
return;
}
int mid=(tree[rt].l+tree[rt].r)>>;
if(r<mid)
query(l,r,rt<<);
else if(l>mid)
query(l,r,rt<<|);
else
{
query(lson);
query(rson);
}
}
int main()
{
int n,i,t,m;
while(scanf("%d",&n)!=EOF)
{
build(,n-,);
sum=;
for(i=;i<n;i++)
{
scanf("%d",&s[i]);
query(s[i],n-,);
updata(s[i],);
}
m=sum;
for(i=;i<n;i++)
{
sum=sum-s[i]+n--s[i];
if(m>sum)
m=sum;
}
printf("%d\n",m);
}
return ;
}

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

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

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

  2. HDU 1394 Minimum Inversion Number(最小逆序数 线段树)

    Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...

  3. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  4. ACM Minimum Inversion Number 解题报告 -线段树

    C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  5. 【hdu1394】Minimum Inversion Number

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

  6. [hdu1394]Minimum Inversion Number(树状数组)

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

  7. HDU-1394 Minimum Inversion Number 线段树+逆序对

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

  8. Minimum Inversion Number

    Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  9. 逆序数2 HDOJ 1394 Minimum Inversion Number

    题目传送门 /* 求逆序数的四种方法 */ /* 1. O(n^2) 暴力+递推 法:如果求出第一种情况的逆序列,其他的可以通过递推来搞出来,一开始是t[1],t[2],t[3]....t[N] 它的 ...

  10. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

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

随机推荐

  1. CentOS VPS创建pptpd VPN服务

    原文地址http://www.hi-vps.com/wiki/doku.php?id=xen_vps_centos6_install_pptpd CentOS VPS创建pptpd VPN服务 Xen ...

  2. C字符串和C++中string的区别 &amp;&amp;&amp;&amp;C++中int型与string型互相转换

    在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别:   C字符串 string对象(C++) 所需的头文件名称 ...

  3. 深入理解jQuery插件开发(转)

    转自:http://blog.jobbole.com/30550/ 如果你看到这篇文章,我确信你毫无疑问会认为jQuery是一个使用简便的库.jQuery可能使用起来很简单,但是它仍然有一些奇怪的地方 ...

  4. Junit3.8

    使用Junit的最佳实践:新建一个名为test的source folder,用于存放测试类源代码目标类与测试类应该位于同一个包下面,这样测试类中就不必导入源代码所在的包,因为他们位于同一个包下面 测试 ...

  5. 第二百四十天 how can I 坚持

    在家待了一天,晚上出去买了个帽子,还有买了点排骨炖着吃了... 玩了好多局游戏. 想搞个直播,不知道能不能玩的起来. 水平太菜了,明天去小米之家玩玩. 睡觉.

  6. notepad++汉字突然横过来了

    修改notepad++,汉字突然横过来了,如图, 百度了一下,原来是因为选择的字体"@微软雅黑"前面的@符号惹的祸,改成"微软雅黑"就没事了.

  7. JavaScript——对this指针的新理解

    一直以来对this的理解只在可以用,会用,却没有去深究其本质.这次,借着<JavaScript The Good Parts>,作了一次深刻的理解.(所有调试都可以在控制台中看到,浏览器F ...

  8. CentOs中iptables配置允许mysql远程访问

    在CentOS系统中防火墙默认是阻止3306端口的,我们要是想访问mysql数据库,我们需要这个端口,命令如下: 1 /sbin/iptables -I INPUT -p tcp --dport 30 ...

  9. 对于一个网站,如何禁止直接从浏览器Web browser中访问js文件

    比如有一个网站,https://testsystem.infotest.com 在这个网站的内容文件目录下面,有一个scripts文件夹,该文件夹中有一个js文件,比如lukeTest.js文件 这样 ...

  10. XML操作:2.LINQ TO XML(http://www.cnblogs.com/AlexLiu/archive/2008/10/27/linq.html)

    LINQ to XML 建立,读取,增,删,改   LINQ to XML的出现使得我们再也不需要使用XMLDocument这样复杂的一个个的没有层次感的添加和删除.LINQ可以使的生成的XML文档在 ...