Editorial for CEOI '16 P2 - Kangaroo


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

A sequence of jumps from cs to cf can be mirrored and thus obtaining a valid sequence of jumps from cf to cs. Due to this symmetry, cs and cf can be swapped such that cs<cf.

We will add the following definitions:

  • A[n][i][j]= the number of alternating permutations that start ascending, having the first and the last elements i and j
  • D[n][i][j]= the number of alternating permutations that start descending, having the first and the last elements i and j
  • X[n][i][j]=A[n][i][j]+D[n][i][j], (our target)
  • Y[n][i][j]=A[n][i][j]D[n][i][j], (for formal reasons)

Let's consider an alternating permutation of length n that starts with i and ends with j. By removing the left extremity (i) and decreasing all values greater than i by 1, we'll obtain an alternating permutation of order n1. We can infer the following recurrences:

A[n][i][j]=k=in2D[n1][k][j1]D[n][i][j]=k=1i1A[n1][k][j1]

In other words, the number of alternating permutations of length n starting ascending with i and ending with j is equal to the number of alternating permutations of length n1 starting descending with i,i+1,,n1 and ending with j1. Similarly for D[][][].

The above recurrences can be rewritten more conveniently:

A[n][i][j]=A[n][i1][j]D[n1][i1][j1]D[n][i][j]=D[n][i1][j]+A[n1][i1][j1]

and from here we obtain:

X[n][i][j]=X[n][i1][j]+Y[n1][i1][j1]Y[n][i][j]=Y[n][i1][j]X[n1][i1][j1]

After a few manipulations, we can further derive:

X[n][i][j]n3,i3=2×X[n][i1][j]X[n][i2][j]X[n2][i2][j2]

Let's stop for a moment to assess the complexity. The answer can be easily computed in O(N3) using the above recurrence, but it can be reduced to O(N2) as follows.

Note the following invariant that is preserved by the recurrence:

nj=(n2)(j2)=constant

This is a key observation that shows the first and the third index of X[][][] will not be independent of each other by repeatedly using the recurrence starting backwards from X[N][cs][cf]. Therefore, instead of three independent variables, (n,i,j), we'll have only two (since Ncf=nj=constant), so the complexity will be O(N2) for a proper implementation.

We have one more thing to do, namely handling the corner cases i2:

Copy
Case n mod 2 = 1
    X[n][1][j] = A[n][1][j] = A[n][j][1]
    A[n][j][1] = D[n-1][j][1] + D[n-1][j+1][1] + … + D[n-1][n-1][1]
    A[n][j][1] = A[n-1][1][j] + A[n-1][1][j+1] + … + A[n-1][1][n-1]
    A[n][1][j] = A[n][1][j-1] - A[n-1][1][j-1]
Case n mod 2 = 0
    X[n][1][j] = A[n][1][j] = D[n][j][1]
    D[n][j][1] = A[n-1][j-1][1] + A[n-1][j-2][1] + … + A[n-1][3][1]
    D[n][j][1] = A[n-1][1][j-1] + A[n-1][1][j-2] + … + A[n-1][1][3]
    A[n][1][j] = A[n][1][j-1] + A[n-1][1][j-1]
resulting in
    X[n][1][j] = X[n][1][j-1] - X[n-1][1][j-1], n mod 2 = 1
    X[n][1][j] = X[n][1][j-1] + X[n-1][1][j-1], n mod 2 = 0

We have also:

Copy
A[n][2][j] = A[n][1][j] - D[n-1][1][j-1] = A[n][1][j] = X[n][1][j]
D[n][2][j] = D[n][1][j] + A[n-1][1][j-1] = A[n-1][1][j-1] = X[n-1][1][j-1]
(there is no descending permutation starting with 1)
    X[n][2][j] = X[n][1][j] + X[n-1][1][j-1]

Comments

There are no comments at the moment.