数据的离散化处理和STL处理方式

来源:互联网 发布:梁家辉 帅 知乎 编辑:程序博客网 时间:2024/04/29 18:35

以下内容来自转载:

有些数据本身很大, 自身无法作为数组的下标保存对应的属性。

如果这时只是需要这堆数据的相对属性, 那么可以对其进行离散化处理!

离散化:当数据只与它们之间的相对大小有关,而与具体是多少无关时,可以进行离散化。

例如

9 1 0 5 4 与 5 2 1 4 3 的逆序对个数相同。
设有4个数:
1234567、123456789、12345678、123456
排序:123456<1234567<12345678<123456789
           =>     1     <        2       <        3       <        4
那么这4个数可以表示成:2、4、3、1

使用STL算法离散化:
思路:先排序,再删除重复元素,然后就是索引元素离散化后对应的值。
假定待离散化的序列为a[n],b[n]是序列a[n]的一个副本,则对应以上三步为:

<span class="sh_function" style="font-weight: bold;"></span><pre class="sh_cpp sh_sourceCode" name="code" style="white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New', Courier, monospace; background-color: white;"><span class="sh_function" style="font-weight: bold;">sort</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>sub_a<span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>sub_a<span class="sh_symbol" style="color: rgb(139, 0, 0);">+</span>n<span class="sh_symbol" style="color: rgb(139, 0, 0);">);</span>
<span class="sh_type" style="color: rgb(0, 100, 0);">int</span> size<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_function" style="font-weight: bold;">unique</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>sub_a<span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>sub_a<span class="sh_symbol" style="color: rgb(139, 0, 0);">+</span>n<span class="sh_symbol" style="color: rgb(139, 0, 0);">)-</span>sub_a<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span><span style="font-family: Georgia, 'Bitstream Charter', serif;">//size为离散化后元素个数</span>
<span class="sh_keyword" style="color: blue; font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);"><</span>n<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">++)</span><span style="white-space: pre;"></span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]=</span><span class="sh_function" style="font-weight: bold;">lower_bound</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>sub_a<span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>sub_a<span class="sh_symbol" style="color: rgb(139, 0, 0);">+</span>size<span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">])-</span>sub_a <span class="sh_symbol" style="color: rgb(139, 0, 0);">+</span> <span class="sh_number" style="color: purple;">1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span><span style="font-family: Georgia, 'Bitstream Charter', serif;">//k为b[i]经离散化后对应的值</span>

对于第3步,若离散化后序列为0, 1, 2, ..., size - 1则用lower_bound,从1, 2, 3, ..., size则用upper_bound,其中lower_bound返回第1个不小于b[i]的值的指针,而upper_bound返回第1个大于b[i]的值的指针,当然在这个题中也可以用lower_bound然后再加1得到与upper_bound相同结果,两者都是针对以排好序列。使用STL离散化大大减少了代码量且结构相当清晰。


0 0
原创粉丝点击