Editorial for IOI '07 P1 - Aliens


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.

We'll use the terms red cells and red squares for cells and squares with flattened grass. The other cells and squares are white. Using the alien device at some coordinates is now equivalent to checking the color of a cell.

The process of finding the solution consists of two stages. The first stage is to determine the side length M of one square of the chessboard and the center (XC,YC) of the red square F containing the given red cell (X0,Y0). The second stage is to move from (XC,YC) to the center of the entire crop formation (using shifts of length M or 2M along each coordinate).

The key part of this task is efficiently solving the first stage. In order to find M, we should find:

  • XR — the rightmost x-coordinate of cells in F,
  • XL — the leftmost x-coordinate of cells in F, and
  • YB — the bottom y-coordinate of cells in F.

We describe several approaches for finding XR; it is trivial to adapt them for finding XL and YB.

The most obvious algorithm checks the colors of cells (X0+1,Y0), (X0+2,Y0), (X0+3,Y0) etc., stopping at the first white cell (XR+1,Y0) and using M queries in the worst case. This approach would score 40 points, as described in the Grading section of the task description.

To cut down on the number of queries, let's instead check the color of cells (X0+1,Y0),(X0+2,Y0),(X0+4,Y0),,(X0+2k,Y0),, stopping at the cell we call Clast. This is the first white cell of the sequence or the first cell outside the meadow boundaries (we must be careful not to query the device in the latter case). It can be shown that all red cells left of Clast must belong to the square F and not to some other red square. Let Cprevious be the rightmost red cell left of Clast. We have used at most log2M queries so far.

Now we could reset X0=Cprevious and repeat the procedure described in the last paragraph until [Cprevious,Clast] contains only 2 cells, after which Cprevious=(XR,Y0). However, here we have used (log2M)2 queries in the worst case, which is only good enough to score 70 points.

To get the full score, use binary search on the segment [Cprevious,Clast] to find XR:(XR,Y0) is the only red cell in that segment that has a neighboring white cell to the right. Using this algorithm we can find all of XR,XL and YB using at most 6log2M queries total. Now, M=XRXL+1,XC=XL+(M1)/2,YC=YB+(M1)/2.

This concludes the description of the first stage. The second stage uses the alien device only a few times to detect which of 13 possible squares has (XC,YC) as the center. Just checking the color of several cells having XC,XC±M,XC±2M as x-coordinates and YC,YC±M,YC±2M as y-coordinates will do the trick.


Comments

There are no comments at the moment.