Problem 2: Synchronizing Lists
https://open.kattis.com/problems/synchronizinglists
First, we need to know the correspondence of between the first list and second list. In other words, which element in the first list matches with which element in the second list? The easiest way to do this is to sort both lists. Then, the -th element of the sorted first list matches with the -th element of the sorted second list.
Now, we print out the second list so that it has the same ordering as the first list. We know that in the printed list, the -th element of the sorted second list has the same index as the -th element of the unsorted first list. Thus, we can iterate for to to fill the indexes of the printed list.
Implementation Tips:
To make your life simpler, initialize the printed list so that it starts out with a size of . That way, you don't have to add to the printed list in order.
# Declare a vector of size n
vector<int> vect(n);
You may also find the find
command useful. However, it returns an iterator, so to get an index from it, you must subtract another iterator, which is usually the beginning iterator.
# Example usage
vector<int> vect = {3, 6, 9, 12, 15};
cout << find(vect.begin(), vect.end(), 12) - vect.begin() << endl;
# Prints out 3
Last updated