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. WCF服务的IIS托管(网站托管)

    基本思路 1.新建WCF应用程序2.注册路由(可省略,则用/….svc/….访问)配置文件 <appSettings> <add key="aspnet:UseTaskFr ...

  2. iOS 监听控件某个属性的改变observeValueForKeyPath

    创建一个测试的UIButton #import "ViewController.h" @interface ViewController () @property(nonatomi ...

  3. 图像滤镜艺术---ZPhotoEngine超级算法库

    原文:图像滤镜艺术---ZPhotoEngine超级算法库 一直以来,都有个想法,想要做一个属于自己的图像算法库,这个想法,在经过了几个月的努力之后,终于诞生了,这就是ZPhotoEngine算法库. ...

  4. C#根据对象的指定字段去除重复值

    PersonInfo类: public class PersonInfo { public int Index; public string Name; public override string ...

  5. oracle data guard备库备份恢复

    客户有套data guard环境,主库在阿里云上,备库在本地机房,现在想定期做备份,但是因为一些原因,备份阿里云上的主库实现会有些问题,所以只能备份本地的备库.目前需求就是测试备库的备份文件是否可以进 ...

  6. 安卓环境下,通过QT调用jar包

    在安卓上,许多第三方工具都提供jar包.qt可以通过jni来调用jar包.本文通过一个例子,说明安卓上QT调用jar的方式. 工具/原料   qt android jar包 jar包准备   1 ja ...

  7. Unity开发概览(HoloLens开发系列)

    本文翻译自:Unity development overview 要开始使用Unity创建全息应用,点此安装包含Unity HoloLens技术预览的开发工具.Unity HoloLens技术预览基于 ...

  8. KM算法 详解+模板

    先说KM算法求二分图的最佳匹配思想,再详讲KM的实现.[KM算法求二分图的最佳匹配思想] 对于具有二部划分( V1, V2 )的加权完全二分图,其中 V1= { x1, x2, x3, ... , x ...

  9. 解决Linux和Windos不同步的问题

    两种方式: 一:在windos上进行操作      (1).cmd中输入如下命令      Reg add HKLM\SYSTEM\CurrentControlSet\Control\TimeZone ...

  10. Spring Boot2(一):使用Spring Boot2集成Mybatis基础搭建

    Mybatis 初期使用比较麻烦,需要各种配置文件.实体类.Dao 层映射关联.还有一大推其它配置.mybatis-spring-boot-starter 就是 Spring Boot+ Mybati ...