Gathering Search Results
InterviewTime limit3sMemory limit1024 MB
Given k search engine rankings of n results, output one combined ranking that minimizes the sum of squared position differences across all engines.
Problem
You are making a new search engine for algorithms, called the Benelux Algorithm Preview Collector. All the marketing has been done, and you have plenty of investors, but you have not written any code yet. With only five hours left until the product launch, there is not enough time to implement your own ranking algorithm. Instead, whenever a user searches for an algorithm, you forward the search to the most popular other search engines and use their results.
Each of these search engines gives a list of results, ordered by estimated relevance. For every search term you try, the search engines always return the same results, and only the order differs between them. You need to combine the rankings into a single ranking.
Give each possible combined ranking a cost. For each result and search engine , if your combined ranking puts at position and search engine puts at position , the cost is . The total cost of a combined ranking is the sum of all costs.
As an example, consider the second sample case. The first search engine returns the same result as the sample output, which has cost . The second search engine swaps two adjacent results, which has cost . For the third search engine, the first result moves two positions back, and the other two shift one position to the front, which has cost . The total cost is , which is the minimal possible cost over all possible orders.
What is the order in which BAPC should output the results, such that the total cost is minimized?
Input
The input consists of:
- One line containing two integers and (, ), the number of results and the number of search engines.
- lines, each containing integers, denoting the results as ordered by one of the search engines. Each line contains every integer from to exactly once.
Output
Output the combined order of the results, such that the cost is minimized.
If there are multiple valid solutions, you may output any one of them.