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. 网络库Asio交叉编译(Linux生成ARM)

    1.  Asio是一个跨平台的C++库,用于网络和底层I/O编程.Asio使用先进的C++方式提供了一系列的异步模型 2. 官方网址:http://think-async.com 3. 由于Asio库 ...

  2. DELPHI下多线程编程的几个思维误区(QDAC)

    有几个网友私下问我一些有关线程的事情.过节写个东西上来大家交流. 思维误区1,自己新建的THREAD是线程,自己的主程序不是线程. 很多人在多线程编程没有把主线程也当作线程.其实主线程也是线程.看起来 ...

  3. WPF DataGrid自定义列DataGridTextColumn.ElementStyle和DataGridTemplateColumn.CellTemplate

    <Window x:Class="DataGridExam.MainWindow"        xmlns="http://schemas.microsoft.c ...

  4. StackExchange.Redis 封装类

    using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using ...

  5. ELK日志系统:Elasticsearch + Logstash + Kibana 搭建教程 good

    环境:OS X 10.10.5 + JDK 1.8 步骤: 一.下载ELK的三大组件 Elasticsearch下载地址: https://www.elastic.co/downloads/elast ...

  6. VS2008发布项目“发布失败”,没有提示错误

    VS2008发布项目时发布失败,但是没有提示任何的错误. 解决方法: 组合键”Ctrl+Alt+O”; 根据这个我知道了,是因为我更改了文件名,发布时找不到导致的,然后在资源管理器中找到那一项,删除或 ...

  7. WPF WindowChrome 自定义窗口

    1.wpf自定义窗口: WindowChrome类描述:https://msdn.microsoft.com/zh-cn/library/system.windows.shell.windowchro ...

  8. 高斯判别分析模型( Gaussian discriminant analysis)及Python实现

    高斯判别分析模型( Gaussian discriminant analysis)及Python实现 http://www.cnblogs.com/sumai 1.模型 高斯判别分析模型是一种生成模型 ...

  9. Go语言v1.8正式发布,有显著的性能提升和变化(go适合服务器编程、网络编程)

    前言 Go语言现在在服务端的网络编程领域越来越火,尤其像IM即时通讯应用这种富网络应用且对服务端网络性能要求极高的场景,很高兴看到Golang发布了1.8正式版,希望在多核架构横行的时代多一些这种顺应 ...

  10. SIP:用Riverbank的SIP创建C++库的Python模块(把自己的C++库包装成Python模块)

    我们发现PyQt做的Python版的PyQt是如此好用,如果想把自己的C++库包装成Python模块该如何实现呢? 这里介绍下用SIP包装C++库时值得参考的功能实现: 需要Python模块中实现C+ ...