Finding a Home
InterviewTime limit1sMemory limit1024 MB
For each query L R, people L to R may swap houses among themselves. Print the sequence that maximizes the sum of house index times person number.
- Level
Easy3 of 10
- Topics
- Greedy, Sorting, Implementation
- Solved
- No attempts yet
Problem
Seogang University has empty houses. The university wants to assign people so that each person moves into exactly one house. For each person who moves into house , the university reduces their tax by .
The assignment is given as a sequence of integers from to , where each value appears once. This means house is assigned to person .
Write a program that solves the following query.
- : Starting from the initial assignment, the people numbered through may swap the houses currently assigned to them with each other as many times as the university wants. After reassigning, the total tax reduction is maximized. Output the sequence .
Every query is independent. After each query, the sequence returns to its initial state.
Input
The first line contains the length of the sequence.
The second line contains the sequence , separated by spaces.
The third line contains the number of queries .
The next lines each contain two integers and , separated by a space.
Output
For each query, print the answer on its own line.
Each line contains the sequence after the query is finished, separated by spaces.
If there are several valid answers, print any one of them.