数据的离散化处理和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
- 数据的离散化处理和STL处理方式
- 数据的离散化处理
- 数据的离散化处理
- 采用离散化处理方式
- poj2528离散化处理数据 线段树
- 数据的离散化 STL用法
- 坐标离散化处理
- 离散化处理
- [数据与处理]归一化(连续值和离散值)
- 一个人的旅行-离散化处理
- 朴素贝叶斯法(对于连续和离散属性的处理)
- 数字信号处理——离散信号的DTFT和DFT
- 等值线处理程序(离散数据)
- USACO RECT1 离散化处理
- Label显示数据过长的处理方式
- ibatis中处理数据的方式(特殊)
- 计算机底层数据的处理方式
- 数据库图片数据的处理方式
- 网络管理
- EA建表简析
- BRD、MRD、PRD文档撰写工具介绍-----产品经理深入浅出课程
- 1001-奖券数目
- 有道云笔记网页版(Cloud notes page)
- 数据的离散化处理和STL处理方式
- 深入理解Java回收机制
- Android性能优化
- df和du磁盘空间不一致
- (6)Python爬虫——爬取中新网新闻
- 隐藏oracle命令行登陆密码
- 搭建高质量的Android项目框架系列四
- nginx支持负载均衡及https安装配置
- 歌曲和商品推荐背后的算法