//返回离散化后的宽度 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(); }
核心思想:排序后去重