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

Minimum Inversion Number

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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 5005
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
struct node
{
int l,r,value;
}tree[N<<];
int a[N];
/*
求移位后的最小逆序数
Update单点增减
Query区间求和
*/
void Build(int rt,int l,int r)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].value=;
if(l==r) return ;
int m=(l+r)>>;
Build(rt<<,l,m);
Build(rt<<|,m+,r);
} void Update(int rt,int num)
{
if(tree[rt].l==num&&tree[rt].r==num){
tree[rt].value=;
return ;
}
int m=(tree[rt].l+tree[rt].r)>>;
if(num<=m) Update(rt<<,num);
else Update(rt<<|,num);
tree[rt].value=tree[rt<<].value+tree[rt<<|].value;
} int Query(int rt,int l,int r)
{
/*
画个图就明白了,搜寻的区间为[l,r],m代表当前节点的左儿子和右儿子的分支
如果左儿子有包含该区间的元素,即l<=m,那么就要继续递归搜寻左儿子
如果右儿子有包含该区间的元素,即m<r,那么就要继续递归搜寻右儿子
当当前的节点的区间被搜寻的区间[l,r]覆盖的话,就说明该段区间完全在[l,r]里面
那么返回该节点的值
*/
if(l<=tree[rt].l&&tree[rt].r<=r){
return tree[rt].value;
}
else{
int m=(tree[rt].l+tree[rt].r)>>;
int s1=,s2=;
if(l<=m) s1=Query(rt<<,l,r);
if(r>m) s2=Query(rt<<|,l,r);
return s1+s2;
}
} int main()
{
int n;
while(~scanf("%d",&n)){
Build(,,n);
int sum=;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
a[i]++;
sum+=Query(,a[i]+,n);
Update(,a[i]);
}
int res = sum;
for(int i=n-;i>=;i--){
sum=sum-(n-a[i])+(a[i]-);
if(sum<res) res=sum;
}
printf("%d\n",res);
}
return ;
}

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 1116 敌兵布阵 线段树 区间求和 单点更新

    线段树的基本知识可以先google一下,不是很难理解 线段树功能:update:单点增减 query:区间求和 #include <bits/stdc++.h> #define lson ...

  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 线段树

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

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

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

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

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

随机推荐

  1. WPF异常捕获,并使程序不崩溃!

    原文:WPF异常捕获,并使程序不崩溃! 在.NET中,我们使用try-catch-finally来处理异常.但,当一个Exception抛出,抛出Exception的代码又没有被try包围时,程序就崩 ...

  2. NSURLSession 网络库 - 原生系统送给我们的礼物

    大家在进行iOS开发的时候一定会用到网络操作.但由于早期原生的 NSURLConnection 操作起来有很多不便,使得大家更愿意使用第三方库的解决方案,比如鼎鼎大名的 AFNetworking.正是 ...

  3. 两个同名controller导致调用崩溃

    之前遇到一个很诡异的bug,大概情况如下: 生成成功,运行正常,调试正常 但是调用目标controller的目标方法,运行自动中断,调试自动中断 没有任何明确的错误提示,包括调试都没有弹窗报错 调用其 ...

  4. 简化连接Buffer对象的过程

    上述一大段代码仅只完成了一件事情,就是连接多个Buffer对象,而这种场景需求将会在多个地方发生,所以,采用一种更优雅的方式来完成该过程是必要的.笔者基于以上的代码封装出一个bufferhelper模 ...

  5. DirectX 图形流水线

    Direct3D 的可编程流水线用来为实时游戏渲染图形(一个词概括——实时渲染) 上面的图是Dx11的实时渲染流水线,Dx的几个版本都是向下兼容的. Input-Assembler Stage: 输入 ...

  6. PHP中的序列化

    接口 Serializable { abstract public string serialize (void); abstruact public void unserialize (string ...

  7. jquery li练习2-恢复链条

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  8. MVC WebApi

    两种web服务 •SOAP风格:基于方法,产品是WebService •REST风格:基于资源,产品是WebAPI 对于数据的增.删.改.查,提供相对的资源操作,按照请求的类型进行相应处理,主要包括G ...

  9. Win8 Metro(C#)数字图像处理--3.3图像直方图计算

    原文:Win8 Metro(C#)数字图像处理--3.3图像直方图计算 /// <summary> /// Get the array of histrgram. /// </sum ...

  10. HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。

    解决方法: 找到目录浏览,打开,点击右边的启用就OK了.