[原创]HDU 1394 Minimum Inversion Number [线段树-> 单点更新]【数据结构】
2016-11-13 16:27:43 Tabris_ 阅读数:237
博客爬取于 2020-06-14 22:42:38
以下为正文
版权声明:本文为 Tabris 原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/53149713
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394
--------------------------------------------------.
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18798 Accepted Submission(s): 11367
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
Author
CHEN, Gaoli
Source
ZOJ Monthly, January 2003
---------------------------------------------------.
题目大意:
就是有一个序列 ,这个序列可以循环,变成下列这么些个序列。
a1, a2, ..., an-1, an (m = 0 - the initial seqence)
a2, a3, ..., an, a1 (m = 1)
a3, a4, ..., an, a1, a2 (m = 2)
...
an, a1, a2, ..., an-1 (m = n-1)
然后对于每种序列 计算逆序对数
然后输出逆序对数最小的值
解题思路:
首先对于最初的序列求解逆序对数 ,只需要线段树 or 树状数组就行了
这里采取的是线段树维护
在每次把数据挂到树上之前 可以区间查询的方式计算这个值的逆序数 然后 O(n)遍历一遍就能知道整个序列的逆序数了
总复杂度是 O(nlogn)
然后他要求的序列一共有 n 个那样的话不能 O(n^2logn) 这样复杂度实在太高了
然后想最后发现了有一个规律;
因为序列中的数就是 0~n-1 且新的序列是从旧的序列移过来的。
那么逆序数就会相应减少a_i个 同时就增加了n-a_i-1个
这样的话就能够 O($1$)处理了
附本题代码
------------------------------------.
1 | # include <bits/stdc++.h> |


