数组离散化模板

代码:

对a数组离散化
void Discretization(int *a,int n)
{
    int b[n+2];
    for(int i=1;i<=n;i++) b[i]=a[i];
    sort(b+1,b+n+1);
    int size=unique(b+1,b+n+1)-b-1;//size为离散化后元素个数
    //cout<<"size="<<size<<endl;
    for(int i=1;i<=n;i++)
        a[i]=lower_bound(b+1,b+size+1,a[i])-b ;//k为b[i]经离散化后对应的值
}

  

猜你喜欢

转载自www.cnblogs.com/liuyongliu/p/10306474.html