Search code examples
algorithmdynamic-programminggreedydivide-and-conquer

Permuting rows in an array to eliminate increasing subsequences


The following problem is taken from Problems on Algorithms (Problem 653):

You are given a n x 2 matrix of numbers. Find an O(n log n) algorithm that permutes the rows in the array such that that neither column of the array contains an increasing subsequence (that may not consist of contiguous array elements) longer than ⌈√n.⌉

I'm not sure how to solve this. I think that it might use some sort of divide-and-conquer recurrence, but I can't seem to find one.

Does anyone have any ideas how to solve this?


Solution

  • Heres's my solution.

    1) Sort rows according to the first element from greatest to lowest.

    1 6    5 1
    3 3 -\ 3 3
    2 4 -/ 2 4
    5 1    1 6
    

    2) Divide it into groups of ⌈√n⌉, and what is left(no more then ⌈√n⌉ groups)

    5 1    5 1
    3 3 -\ 3 3
    2 4 -/ 
    1 6    2 4
           1 6
    

    3) Sort rows in each group according to the second element from greatest to lowest

    5 1    3 3
    3 3    5 1
        -> 
    2 4    1 6
    1 6    2 4
    

    Proof of correctness:

    Increasing subsequences in column 1 can happen only in single group(size is <= ⌈√n⌉),

    No 2 elements of increasing subsequences in column 2 are in the same group(no more than ⌈√n⌉ groups)