vector < int > scale(vector < int > v) { vector < int > X = v; map < int, int > M; sort(X.begin(), X.end()); X.erase(unique(X.begin(), X.end()), X.end()); int nc = 0; for (int i = 0; i < X.size(); ++i) M[X[i]] = nc++; for (int i = 0; i < v.size(); ++i) v[i] = M[v[i]]; }Hope you will find this one helpful!
This is a blog by Szymon Sidor. Its original purpose was to present nontrival Computer Science and Mathematical problems in an accessible way, but it evolved and now diverse topics are covered.
LateX
Thursday, October 13, 2011
Scaling coordinates
Sometimes we want to iterate over all coordinates of points in some space. In many cases the distance between points is not important, what is important is their relative order of occurrence along axis. Then we can scale {-5, 0, 5} to {0,1,2} for example along x-axis. Quite often this makes it possible to implement a static interval tree along the axis, which can be useful for some application. Here is really nice an compact code in C++ that achieves that:
Subscribe to:
Posts (Atom)