Source: EGMO-2016-P5
Category: Extremal Problems

Problem: "Let integers $k,n$ satisfy $k\\geq2$, $k\\leq n\\leq 2k-1$. Place some $1\\times k$ or $k\\times 1$ rectangular cards on an $n\\times n$ grid such that each card covers exactly $k$ cells, no two cards overlap, and no additional card can be placed. Determine the minimum possible number of cards placed among all such placements.
"

Answer: When $n=k$ it is $n$, and when $k+1\le n\le 2k-1$ it is $\min\{n,2n-2k+2\}$

Reference solution:
The minimum possible number of cards is $n$ when $n=k$, and $\min\{n, 2n-2k+2\}$ when $k+1 \le n \le 2k-1$.

**Constructions:**
When $n=k$, a total of $n$ pieces can be placed by filling the grid with $n$ horizontal pieces (one in each row) or $n$ vertical pieces (one in each column). Because a piece of length $k$ in a $k\times k$ grid spans an entire row or column, placing even one horizontal piece prevents any vertical piece from being placed, and vice versa. Thus, all pieces must be of the same orientation, and a maximal placement requires exactly $n$ pieces.

When $k+1 \le n \le 2k-2$, $2n-2k+2$ pieces can be placed as follows: 
In the upper-left $(k+1)\times(k+1)$ square, place $4$ pieces arranged in a pinwheel fashion: a $1\times k$ piece in row $1$, columns $1$ to $k$; a $k\times 1$ piece in column $k+1$, rows $1$ to $k$; a $1\times k$ piece in row $k+1$, columns $2$ to $k+1$; and a $k\times 1$ piece in column $1$, rows $2$ to $k+1$. This leaves a central $(k-1)\times(k-1)$ empty square. For the remaining grid, the next $n-k-1$ rows (rows $k+2$ to $n$) each contain one $1\times k$ horizontal piece in columns $1$ to $k$, and the next $n-k-1$ columns (columns $k+2$ to $n$) each contain one $k\times 1$ vertical piece in rows $1$ to $k$. It is easy to verify that no additional piece can be inserted, as the remaining empty spaces are too small to accommodate a $k$-length piece. This uses exactly $4 + 2(n-k-1) = 2n-2k+2$ pieces.

When $n=2k-1$, $n$ pieces can be placed as follows: all pieces are placed vertically, alternating between the topmost and bottommost positions of each column. Specifically, for each column $j$ from $1$ to $n$, place a $k\times 1$ piece in rows $1$ to $k$ if $j$ is odd, and in rows $k$ to $2k-1$ if $j$ is even. It is easy to verify that no additional piece can be inserted at this stage, as each column has only $k-1$ empty cells, and any row has at most $1$ consecutive empty cell (row $k$ has none), meaning no $1\times k$ or $k\times 1$ piece can fit.

**Proof of the Lower Bound:**
When $k+1 \le n \le 2k-1$, we prove that any placement satisfying the requirements contains at least $\min\{n, 2n-2k+2\}$ pieces. Let $r$ be the number of rows that do not contain any horizontal piece, and $c$ be the number of columns that do not contain any vertical piece. 

If $r=0$ or $c=0$, then at least $n$ pieces are placed. (For instance, if $r=0$, every row contains at least one horizontal piece. Since a horizontal piece is contained entirely in one row, there must be at least $n$ horizontal pieces.)

If both $r$ and $c$ are non-zero, we prove that at least $2n-2k+2$ pieces are placed. Consider a horizontal piece $T$. Since $n < 2k$, the top or bottom boundary of the board is at most $k-1$ rows away from the row containing $T$. Assume without loss of generality that the top boundary is closer. Let $y$ be the row containing $T$; then $y-1 \le k-1$. The rows strictly between $T$ and the top boundary (rows $1$ to $y-1$) intersect the $k$ columns occupied by $T$, forming a rectangular region of size $(y-1) \times k$. 

This region cannot accommodate a vertical piece, because any vertical piece intersecting this region must not overlap with $T$, and thus would be restricted to rows $1$ to $y-1$. However, $y-1 < k$, so a $k \times 1$ vertical piece cannot fit. Therefore, no vertical piece intersects this region. Since the placement is maximal, no additional horizontal piece can be placed. Thus, every row in this region must already share at least one cell with a horizontal piece (otherwise, all $k$ cells in that row within the region would be empty, allowing a new horizontal piece to be placed). 

Since a horizontal piece is entirely contained in a single row, this means every row from $1$ to $y-1$ must contain a horizontal piece. This argument shows that for any row containing a horizontal piece, all rows between it and the closest boundary (top or bottom) must also contain a horizontal piece. Consequently, the set of rows containing a horizontal piece forms a union of a prefix of rows and/or a suffix of rows. Therefore, the $r$ rows lacking a horizontal piece must be strictly consecutive. 

Similarly, all $c$ columns lacking a vertical piece are consecutive. The $r$ rows and $c$ columns thus intersect to form an $r \times c$ rectangular region. Because no horizontal piece is in these $r$ rows and no vertical piece is in these $c$ columns, this region contains no pieces at all. Since no new piece can be placed in this empty region, we must have $r < k$ and $c < k$. 

Consequently, there are $n-r \ge n-k+1$ rows that each contain at least one horizontal piece, implying there are at least $n-k+1$ horizontal pieces. Similarly, there are $n-c \ge n-k+1$ columns that each contain at least one vertical piece, implying there are at least $n-k+1$ vertical pieces. Hence, at least $(n-k+1) + (n-k+1) = 2n-2k+2$ pieces are placed in total. This completes the proof.

Grading guidelines:
(Partial) 1. Gave a correct construction achieving the upper bound in at least one nontrivial case (e.g. the pinwheel-based construction with exactly $2n-2k+2$ cards for $k+1\le n\le 2k-2$, or the alternating-columns construction with $n$ cards for $n=2k-1$), and justified maximality of that construction. 2. Proved the structural claim that if both orientations occur in a maximal placement then the set of rows containing a horizontal card is a union of a top prefix and a bottom suffix (equivalently, the rows with no horizontal card form one consecutive block), and analogously for columns/vertical cards. 3. Established a valid lower bound of the right order, e.g. showed that if $r$ rows have no horizontal card and $c$ columns have no vertical card, then the $r\times c$ intersection must be empty and hence $r<k$ and $c<k$, leading to at least $(n-k+1)+(n-k+1)=2n-2k+2$ cards (even if some details/cases are incomplete). (Almost) 1. Correctly derived the lower bound $\ge \min\{n,2n-2k+2\}$ for all $k\le n\le 2k-1$ via the $r,c$ argument (including the consecutive-block property and the conclusion $r,c<k$), but did not fully present/verify a matching construction in one of the subranges. 2. Provided matching constructions for all required ranges and proved the lower bound, but left a small gap in the maximality check (e.g. did not fully justify why no additional horizontal/vertical card fits in the remaining empty region, or omitted a boundary subcase such as $n=k$ or $n=2k-1$). 3. Solution is almost complete, but made minor mistakes which are not negligible.

Construction task:
For the specific case $n=19$ and $k=10$, provide an explicit construction of a valid card placement that achieves the minimum possible number of cards.

Construction requirements:
- Output a single Python list of lists, where each inner list represents a single card.
- Each card should be represented by its two endpoints as a list of two tuples: `[(r1, c1), (r2, c2)]`, where `r` is the row index and `c` is the column index, with `r1 <= r2` and `c1 <= c2`.
- Use 0-based indexing for coordinates on the $19 \times 19$ grid, so `r` and `c` are integers from 0 to 18.
- For example, a card covering from row 0 to 9 in column 0 is `[(0, 0), (9, 0)]`.
- The placement must be unextendable (no additional $1 \times 10$ or $10 \times 1$ card can be placed anywhere on the grid without overlapping existing cards).
- The number of cards must be exactly the minimum possible for this unextendable condition.
- Put ONLY the Python expression (e.g., a literal list or a list comprehension) inside the construction block.

Reference construction:
[[(0, c), (9, c)] if c % 2 == 0 else [(9, c), (18, c)] for c in range(19)]

Verifier code:
import sys

def verify():
    payload = sys.stdin.read().strip()
    if not payload:
        print("Empty input.")
        return

    # Parse the construction payload safely
    try:
        # We supply 'range' in locals to support list comprehensions as seen in the ref_construction
        cards = eval(payload, {"__builtins__": {}}, {"range": range})
    except Exception as e:
        print(f"Failed to parse construction: {e}")
        return

    # 1. Verify the parsed object is a list
    if type(cards) is not list:
        print("Construction must be a list of lists.")
        return

    # 2. Check total number of cards
    if len(cards) != 19:
        print(f"Expected exactly 19 cards, but got {len(cards)}.")
        return

    # Initialize a 19x19 boolean grid to track occupied cells
    grid = [[False] * 19 for _ in range(19)]

    for i, card in enumerate(cards):
        # 3. Verify each card is a list containing exactly two endpoints
        if type(card) is not list:
            print(f"Card at index {i} is not a list.")
            return
        if len(card) != 2:
            print(f"Card at index {i} does not contain exactly two endpoints.")
            return
        
        ep1, ep2 = card
        
        # 4. Verify endpoints are tuples of two integers
        if type(ep1) is not tuple or len(ep1) != 2 or type(ep2) is not tuple or len(ep2) != 2:
            print(f"Endpoints of card {i} must be tuples of length 2.")
            return
            
        r1, c1 = ep1
        r2, c2 = ep2
        
        if not (type(r1) is int and type(c1) is int and type(r2) is int and type(c2) is int):
            print(f"Coordinates of card {i} must be integers.")
            return
            
        # 5. Verify coordinates are within bounds and properly ordered
        if not (0 <= r1 <= r2 <= 18 and 0 <= c1 <= c2 <= 18):
            print(f"Coordinates of card {i} are out of bounds or improperly ordered.")
            return
            
        # 6. Verify dimensions to ensure it covers exactly 10 cells
        if not ((r1 == r2 and c2 - c1 == 9) or (c1 == c2 and r2 - r1 == 9)):
            print(f"Card {i} does not cover exactly 10 cells.")
            return
            
        # 7. Mark cells on the grid and check for overlaps
        for r in range(r1, r2 + 1):
            for c in range(c1, c2 + 1):
                if grid[r][c]:
                    print(f"Card {i} overlaps with an existing card at ({r}, {c}).")
                    return
                grid[r][c] = True

    # 8. Verify the placement is strictly unextendable
    # Scan all 19 rows
    for r in range(19):
        consecutive_false = 0
        for c in range(19):
            if not grid[r][c]:
                consecutive_false += 1
                if consecutive_false >= 10:
                    print(f"Placement is extendable: a 1x10 card can be placed in row {r}.")
                    return
            else:
                consecutive_false = 0

    # Scan all 19 columns
    for c in range(19):
        consecutive_false = 0
        for r in range(19):
            if not grid[r][c]:
                consecutive_false += 1
                if consecutive_false >= 10:
                    print(f"Placement is extendable: a 10x1 card can be placed in column {c}.")
                    return
            else:
                consecutive_false = 0

    # All checks passed
    print("True")

if __name__ == "__main__":
    verify()