This is a question of a programming contest (which has ended). I tried to solve this problem, but could not find a healthy method for this.
The question is this:
IIIT Allahabad celebrates its annual Technocultural Fiesta Effervescence MM12 from October 1 to 5. The chef agreed to provide candy for this holiday season. The chef prepared N boxes of sweets, numbered from 1 to N (each number occurs exactly once). The chef is very concerned about the arrangement of the boxes. He wants the boxes to be organized in a certain order, but, unfortunately, the Chef is busy. He asked you to rearrange the boxes for him. Given the current order of the boxes, you should reorder the boxes in that order. However, there is a limitation. You can exchange only two adjacent fields to achieve the desired order. Conclusion, a minimum number of such adjacent swaps is required.
Enter
The first line of input contains a single integer T, the number of test cases. Each test case contains 3 lines, the first line contains a single integer N, the number of boxes. The next 2 lines contain N numbers each, the first line is the specified order of boxes, and the second line is the desired order.
Output
For each test case, print a single integer "K", the minimum number of contiguous swaps. Limitations:
1<=T<=10
1<=N<=10^5
Example
Input:
4
3
1 2 3
3 1 2
3
1 2 3
3 2 1
5
3 4 5 2 1
4 1 5 2 3
4
1 2 3 4
2 3 4 1
Conclusion:
2
3
6
3
I almost did not know about this issue. Can someone explain the logic of the question?
source
share