Atcoder reorder-cards

Can anyone explain logic and code for this problem

This is a problem of coordinate compression.(You can have look on this topic)
So the problem reduces to removing unnecessary rows and columns.
Lets says we are given some coordinate such as;-
Number of rows=3 , columns=4
1st cell - 3, 4
2nd cell- 1,2

SO here our useless rows is the middle row and useless columns are 0th and 2nd, we need to remove them now.
So our basic approach is to sort the coordinates on basis of x coordinates and y coordinates separately.
So our according to this, our operation for above example would
X coordinates
Y coordinates
Now we will coordinate compress each of them .For each individual unique value we will assign numbers to them starting from 1 to … till required.
so now our coordinates would become:-
X coordinates
1 [ 1 will be assigned 1 only]
2 [Now our 3 will become 2 as the next unique number we can assign to 2 will be 2,as 1 is already assigned to 1]
Now if there would be more coordinates after this like 5 [5 will be compressed to 3] and so on.
So from above you can observe after compressing them , we automatically removed 2nd row as 3rd row was compressed to 2.
Same procedure would be applied to Y coordinates.