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编译helloworld的几个小问题

    1.GCC使用在使用GCC编译程序时,编译过程可以被细分为四个阶段:预处理(Pre-Processing)编译(Compiling)汇编(Assembling)链接(Linking).例如:      ...

  2. BootStrap入门教程 (三) :可重用组件(按钮,导航,标签,徽章,排版,缩略图,提醒,进度条,杂项)

    上讲回顾:Bootstrap的基础CSS(Base CSS)提供了优雅,一致的多种基础Html页面要素,包括排版,表格,表单,按钮等,能够满足前端工程师的基本要素需求. Bootstrap作为完整的前 ...

  3. Java IO流中的File类学习总结

    一.File类概述 File类位于java.io包中,是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹. File类有多种重载的构造方法.File类保存文件或目录的各种 ...

  4. matlab search path

    What Is the MATLAB Search PathThe search path, or path is a subset of all the folders in the file sy ...

  5. Calculating a bearing between points in location-aware apps

    https://software.intel.com/en-us/blogs/2012/11/30/calculating-a-bearing-between-points-in-location-a ...

  6. 【转】Maven实战(九)---模块聚合和继承

    原博文出自于:http://blog.csdn.net/liutengteng130/article/details/47001831   感谢! 类之间有聚合和继承关系,Maven也具备这样的设计原 ...

  7. iOS-default.png启动图片

    我在xcode5下写的代码,我下载了iOS6的模拟器,我用iOS6和iOS7的模拟器切换运行,有的时候可以运行有的时候不可以运行,报错: 2013-11-17 16:49:04.049 sim[474 ...

  8. MVC神韵---你想在哪解脱!(十二)

    追加一条电影信息 运行应用程序,在浏览器中输入“http://localhost:xx/Movies/Create”,在表单中输入一条电影信息,然后点击追加按钮,如图所示. 点击追加按钮进行提交,表单 ...

  9. disque概要

    做项目过程接触到disque,记录一下. disque是redis之父开源的基于内存的分布式作业队列,用c语言实现的非阻塞网络服务器. disque的设计目标:Its goal is to captu ...

  10. Oracle-11g 从表空间删除数据文件

    从表空间删除数据文件前提条件 如果欲从表空间中删除数据文件,那么该数据文件必须为空,否则将报出"ORA-03262: the file is non-empty"的错误.   从表 ...