将数据排序;
在去除相同数据后放入新的数列中;
需要时二分查找;
lower_bound()查找
板子!!!!
代码实现:
// 需要离散化的数组是 a[]
vector<int> tmp;
or:int tmp[n+1];
for (int i = 0; i < n; ++i) {
tmp.push_back(a[i]);
or:tmp[i]=a[i];
}
sort(tmp.begin(), tmp.end());
or:sort(tmp+1,tmp+1+n);
for (int i = 0; i < n; ++i) {
a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) -tmp.begin() + 1;
a[i]=lower_bound(tmp+1,tmp+1+n,a[i])-tmp;
printf("%d ", a[i]);
}