ACM模板——离散化

//返回离散化后的宽度 W,不是vector<int> x的数组大小! 
int compress(vector<int>& x,int W)
{
    vector<int> xs;
    int N = x.size();
    _for(i,0,N)
        for(int d = -1;d <= 1;d ++)
        {
            int t1 = x[i]+d;
            if(1<=t1 && t1<=W)
                xs.pb(t1);
        }
        
    sort(xs.begin(),xs.end());
    xs.erase(unique(xs.begin(),xs.end()),xs.end());
    _for(i,0,N)
        x[i] = find(xs.begin(),xs.end(),x[i]) - xs.begin();
    return xs.size();
}
离散化

 核心思想:排序后去重

猜你喜欢

转载自www.cnblogs.com/Asurudo/p/10657984.html