仍旧在练习线段树中。。这道题一开始没有完全理解搞了一上午,感到了自己的shabi。。

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 15527 Accepted Submission(s): 9471

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

题目大意:

给出一个n,和0-n的一个数列,求这个数列的最小逆序对数,最小逆序对表示,给定数列每次都将第一个数放到最后一个位置,如此变化n次至变换回原状,在这n种不同的数列中,逆序对数的最小值即为所求(N<=5000)每组样例多组数据

明白题意后想到求逆序对的三种方法,第一种暴力法,第二种归并,第三种树状数组和线段树,由于在学习线段树于是果断练习用线段树编写
此处线段树的作用是求出原数列的逆序对
对于每次变换,我们发现,第一个数移到最后一位,只需要在上一步里求出的逆序对减去上一步第一位的数,并加上比上一步第一位要大的数即可

下面是代码:

#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 5005
int sum[maxn<<2];
int a[maxn]; void updata(int now)
{
sum[now]=sum[now<<1]+sum[now<<1|1];
} void build(int l,int r,int now)
{
sum[now]=0;
if(l==r) return;
int mid=(l+r)>>1;
build(l,mid,now<<1);
build(mid+1,r,now<<1|1);
} int query(int L,int R,int l,int r,int now)
{
if(L<=l && r<=R)
return sum[now];
int mid=(l+r)>>1;
int total=0;
if(L<=mid) total+=query(L,R,l,mid,now<<1);
if(R>mid) total+=query(L,R,mid+1,r,now<<1|1);
return total;
} void point_change(int loc,int l,int r,int now)
{
if(l==r)
{
sum[now]++;
return;
}
int mid=(l+r)>>1;
if(loc<=mid)
point_change(loc,l,mid,now<<1);
else
point_change(loc,mid+1,r,now<<1|1);
updata(now);
} int main()
{
int n;
while(~scanf("%d",&n))
{
build(0,n-1,1);
int number=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
number+=query(a[i],n-1,0,n-1,1);
point_change(a[i],0,n-1,1);
}
int ans=number;
for(int i=0;i<n;i++)
{
number+=n-a[i]-a[i]-1;
ans=min(ans,number);
}
printf("%d\n",ans);
}
return 0;
}

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 [线段树求逆序数]

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

  3. hdu - 1394 Minimum Inversion Number(线段树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...

  4. HDU 1394 Minimum Inversion Number(线段树 或 树状数组)

    题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...

  5. HDU 1394 Minimum Inversion Number 线段树

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

  6. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  7. hdu 1394 Minimum Inversion Number (树状数组求逆序对)

    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...

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

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

  9. HDU 1394 Minimum Inversion Number(树状数组/归并排序实现

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

随机推荐

  1. 第20章 DLL高级技术(2)

    20.3 延迟载入DLL 20.3.1延迟载入的目的 (1)如果应用程序使用了多个DLL,那么它的初始化可能比慢,因为加载程序要将所有必需的DLL映射到进程的地址空间.→利用延迟加载可将载入过程延伸到 ...

  2. JMeter学习(二十五)HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults

    Test Plan的配置元件中有一些和HTTP属性相关的元件:HTTP Cache Manager.HTTP Authorization Manager.HTTP Cookie Manager.HTT ...

  3. linux --备份oracle

    1.exp\imp 导入导出命令使用exp username/pwd@sid file=path.dmp owner=user 不导出表数据:rows=n举例:exp iflashbuy/qwerwh ...

  4. SQL语句统计每天、每月、每年的数据

    1.每年select year(ordertime) 年,sum(Total) 销售合计from 订单表group by year(ordertime) 2.每月select year(orderti ...

  5. Hashtable 数据遍历的几种方式

    Hashtable 在集合中称为键值对,它的每一个元素的类型是 DictionaryEntry,由于Hashtable对象的键和值都是Object类型,决定了它可以放任何类型的数据, 下面我就把Has ...

  6. 【MFC】ID命名和数字约定

    ID命名和数字约定 MFC ID 命名和数字约定需要满足以下要求: 提供对 Visual C++ 资源编辑器支持的 MFC 库和 MFC 应用程序中使用的一致的 ID 命名标准. 这样就可以轻松地对程 ...

  7. Linux 进程与线程四(加锁--解锁)

    线程共享进程的内存空间,打开的文件描述符,全局变量. 当有多个线程同事访问一块内存空间或者一个变量.一个文件描述符,如果不加控制,那么可能会出现意想不到的结果. 原子操作 对于我们的高级语言(C语言, ...

  8. Oracle11G安装之后

    本人对oracle还处于摸索阶段,今天安装了一下Oracle11G, 安装之后,后台管理端的登录地址:https://172.16.10.75:1158/em 1.使用之前设置的dba管理员密码账号登 ...

  9. JNI 程序开发

    参考资料: http://blog.csdn.net/wwj_748/article/details/28136061 JNI_最简单的Java调用C/C++代码 http://blog.csdn.n ...

  10. Linux操作系统基础(完结)

    摘要 一.Linux操作系统概述 二.Linux操作系统安装 三.Linux文件系统及文件基础 四.Linux操作系统命令使用基础 五.Linux应用程序的安装与卸载基础 五.用户及进程 六.相关信息 ...