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. 对复杂字典Dictionary<T1,T2>排序问题

    原文:对复杂字典Dictionary<T1,T2>排序问题 //VoltageCount类(电压值对应的数量):    public class VoltageCount    {     ...

  2. c# 全局钩子实现扫码枪获取信息。

    原文:c# 全局钩子实现扫码枪获取信息. 1.扫描枪获取数据原理基本相当于键盘数据,获取扫描枪扫描出来的数据,一般分为两种实现方式. a)文本框输入获取焦点,扫描后自动显示在文本框内. b)使用键盘钩 ...

  3. Wpf发送接收 win32消息

    #region WPF发送和接收win32消息 public const int WM_GETTEXT = 0x0D; public const int WM_SETTEXT = 0x0C; publ ...

  4. FreeNAS 11.0 正式发布,提供 S3 兼容的对象存储服务

    FreeNAS 11.0 正式版已发布,该版本带来了新的虚拟化和对象存储功能.FreeNAS 11.0 将 bhyve 虚拟机添加到其受欢迎的 SAN / NAS.Jail 和插件中,让用户可以在 F ...

  5. Python杂谈: 集合中union和update的区别(Python3.x)

    集合中union和update方法都是将多个可迭代的对象合并,但是返回的结果和对初始对象的影响却不一样 # union() 方法 - a.union(b) 将集合a和集合b取并集,并将并集作为一个新的 ...

  6. UWP入门(六)-- ResourceDictionary 和 XAML 资源引用

    原文:UWP入门(六)-- ResourceDictionary 和 XAML 资源引用 你最希望声明为 XAML 资源的 XAML 元素包括 Style.ControlTemplate.动画组件和 ...

  7. Win10自带应用不喜欢?一条命令全部卸载

    与Win8/Win8.1一样,Win10中继续内置了应用商店,所不同的是Windows10中已升级为通用应用商店,具有跨平台特性.可能有的朋友仍不喜欢使用Modern应用,因为传统桌面应用几乎能够满足 ...

  8. OSChina 周三乱弹 —— 致力于做一名优秀的女程序员鼓励师

    https://my.oschina.net/xxiaobian/blog/848096

  9. SharePoint Add-in Model 介绍 - 引文(先导篇)

    1. SharePoint 平台 如果你已经很熟悉 SharePoint 平台,可跳过本章节. 1.1 SharePoint 是什么 在介绍 Add-in Model 之前,简要提一下 SharePo ...

  10. 漏洞告诉你:商家为什么都乐于提供免(diao)费(yu)WiFi?

    作为一名小微商户,每天我除了要为经营小店忙得焦头烂额,还要想方设法地寻求提升用户体验.于是,我用了号称“营销神器”的某商用WiFi系统...... 然后不可思议的事情发生了:连上此WiFi的手机(包括 ...