HDU 1394 线段树or 树状数组~
生活随笔
收集整理的這篇文章主要介紹了
HDU 1394 线段树or 树状数组~
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Minimum Inversion Number
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 2Sample Output
16 題目大意就是給你 N 個(gè)區(qū)間,里面包含有0~N-1的整數(shù),允許你每次將序列的頭調(diào)到序列尾,讓你求出最小的逆序和, 思路:先用線段樹(shù)or樹(shù)狀數(shù)組預(yù)處理出原先的逆序和,然后再一個(gè)一個(gè)推出,每次操作后所產(chǎn)生的的新的逆序和,就拿樣例來(lái)說(shuō),一開(kāi)始的逆序和是22,如果將1調(diào)到末尾那里的話,原先在1后面比1小的數(shù)有1個(gè),這個(gè)數(shù)是0,而當(dāng)1調(diào)到后面的時(shí)候,在1前面的比一大的數(shù)有8個(gè),為3,6,9,8,5,7,4,2,那么新序列的逆序和為22 - 1 + 8 = 29。以此類推 AC代碼:線段樹(shù)的方法,借鑒了notonlysuccess大神的姿勢(shì):| Memory:?320 KB | ? | Time:?78 MS |
后來(lái)我有用樹(shù)狀數(shù)組做了一下,發(fā)覺(jué)樹(shù)狀數(shù)組一般都要比線段樹(shù)要快~
| Memory:?332 KB | ? | Time:?31 MS |
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/jusonalien/p/4004973.html
總結(jié)
以上是生活随笔為你收集整理的HDU 1394 线段树or 树状数组~的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《21天学通C语言(第7版)》一导读
- 下一篇: 全国计算机等级考试题库二级C操作题100