离散化

有两种方法,只贴出较为简便的一种 stl 实现离散化 

#include<algorithm> // 头文件 
//n 原数组大小 num 原数组中的元素 lsh 离散化的数组 cnt 离散化后的数组大小  

int lsh[MAXN] , cnt , num[MAXN] , n; for(int i=1; i<=n; i++) { scanf("%d",&num[i]); lsh[i] = num[i]; } sort(lsh+1 , lsh+n+1); cnt = unique(lsh+1 , lsh+n+1) - lsh - 1; for(int i=1; i<=n; i++) num[i] = lower_bound(lsh+1 , lsh+cnt+1 , num[i]) - lsh;

 

posted @ 2020-02-27 15:11  _LH2000  阅读(155)  评论(0编辑  收藏  举报