Frog Sort codechef February 2021 long challenge editorial -
There are frogs (numbered through ) in a line. For each valid , the -th frog is initially at the position , it has weight , and whenever you hit its back, it jumps a distance to the right, i.e. its position increases by . The weights of the frogs are pairwise distinct.
You can hit the back of each frog any number of times (possibly zero, not necessarily the same for all frogs) in any order. The frogs do not intefere with each other, so there can be any number of frogs at the same time at each position.
Your task is to sort the frogs in the increasing order of weight using the smallest possible number of hits. In other words, after all the hits are performed, then for each pair of frogs such that , the position of the -th frog should be strictly smaller than the position of the -th frog. Find the smallest number of hits needed to achieve such a state.
Input
- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains a single integer .
- The second line contains space-separated integers .
- The third line contains space-separated integers .
Output
For each test case, print a single line containing one integer ― the smallest number of times you need to hit the backs of the frogs.
Constraints
- for each valid
- for each valid
- no two frogs have the same weight
Subtasks
Subtask #1 (50 points):
Subtask #2 (50 points): original constraints
Example Input
3
3
3 1 2
1 4 5
3
3 2 1
1 1 1
4
2 1 4 3
4 1 2 4
Example Output
3
6
5
Explanation
Example case 1: We can hit the back of the first frog three times.
Example case 2: We can hit the back of the first frog four times, then hit the back of the second frog two times.
Problem Statement-
Solution-
Solution will be posted anytime soon stay connect .
Must join Telegram channel -https://t.me/competitiveProgrammingDiscussion
Comments
Post a Comment