<p>You are given an array of strings <code>names</code>, and an array <code>heights</code> that consists of <strong>distinct</strong> positive integers. Both arrays are of length <code>n</code>.</p>

<p>For each index <code>i</code>, <code>names[i]</code> and <code>heights[i]</code> denote the name and height of the <code>i<sup>th</sup></code> person.</p>

<p>Return <code>names</code><em> sorted in <strong>descending</strong> order by the people&#39;s heights</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> names = [&quot;Mary&quot;,&quot;John&quot;,&quot;Emma&quot;], heights = [180,165,170]
<strong>Output:</strong> [&quot;Mary&quot;,&quot;Emma&quot;,&quot;John&quot;]
<strong>Explanation:</strong> Mary is the tallest, followed by Emma and John.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> names = [&quot;Alice&quot;,&quot;Bob&quot;,&quot;Bob&quot;], heights = [155,185,150]
<strong>Output:</strong> [&quot;Bob&quot;,&quot;Alice&quot;,&quot;Bob&quot;]
<strong>Explanation:</strong> The first Bob is the tallest, followed by Alice and the second Bob.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == names.length == heights.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li>
	<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
	<li><code>1 &lt;= heights[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>names[i]</code> consists of lower and upper case English letters.</li>
	<li>All the values of <code>heights</code> are distinct.</li>
</ul>

class Solution {
public:
    static vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
        constexpr uint16_t IMASK = (1u << 10) - 1u;
        const uint16_t n = names.size();
        for (uint16_t i = 0; i < n; i++)
            heights[i] = (heights[i] << 10) + i;
        sort(heights.begin(), heights.end(), greater<>());
        auto &idxs = heights;
        constexpr uint16_t NO_VAL = IMASK;
        int16_t rem = n - 1u;
        for (uint16_t s = 0; s < n && rem > 0; s++) {
            uint16_t d = idxs[s] & IMASK;
            if (d == NO_VAL) continue;
            --rem;
            if (s != d) {
                string temp = move(names[s]);
                names[s] = move(names[d]);
                idxs[s] = NO_VAL;
                for (uint16_t f = idxs[d] & IMASK; f != s; f = idxs[d = f] & IMASK) {
                    names[d] = move(names[f]);
                    idxs[d] = NO_VAL;
                    --rem;
                }
                names[d] = move(temp);
                idxs[d] = NO_VAL;
            }
        }
        return move(names);
    }
};

auto init = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return 'c';
}();
21388