Sort two arrays based on one, IN-PLACE [C++]

Правка en1, от prac64, 2022-01-11 10:18:01

Hello codeforces,

We all have done the classic question where there are two arrays, lets one denoting user-id and other denoting tenure. Now we need to sort both arrays so users with shortest tenure come first. Ex:

user-ids: [34, 51, 21, 22, 37] tenure : [ 1, 4, 1, 10, 3 ]

answer:

user-ids: [21, 34, 37, 51, 22] tenure: [ 1, 1, 3, 4, 10]

This is pretty straight forward with creating array of pairs and using library sort. However this ends up using O(n) extra memory. Surely if I wrote my own selection-sort or merge-sort, I could manage to do it in in-place by book-keeping the indexes and updating both arrays myself.

My question is, how can we do this in C++ using library sort, inplace, without creating a new array ?

Теги c++, algorithm, implementation

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский prac64 2022-01-11 10:18:27 5 Tiny change: 'ays, lets one denot' -> 'ays, lets say, one denot'
en1 Английский prac64 2022-01-11 10:18:01 802 Initial revision (published)