We need to compute maximum number of non-overlapping dominoes (1x2 or 2x1) that can be placed entirely within the Young diagram (Ferrers diagram). The diagram consists of columns of unit squares arranged in left-justified columns, with column i from bottom has height a_i, and a_1 ≥ a_2 ≥ ... ≥ a_n. The diagram is a set of cells (unit squares). Domino can cover two adjacent cells either horizontally (left-right) or vertically (up-down), and they must lie completely within the diagram (i.e., both cells must be present). We need the maximum cardinality of a set of such dominoes that are pairwise non-overlapping (no shared cells). This is essentially maximum matching in the grid graph induced by the cells, but restricted to a Young diagram shape. But we need an efficient algorithm O(n) or O(n log n), with n up to 300k and heights up to 300k.
We need to compute maximum number of dominoes that can be placed. Since dominoes cover two cells, the maximum number is at most floor(total cells / 2). But due to shape constraints, it could be less. Actually, this is maximum matching in a bipartite graph? The grid graph is bipartite (chessboard coloring). Since the diagram is convex? Young diagram is a shape that is left-justified and decreasing. Might have a formula.
We need to compute maximum matching size in the grid graph of a Ferrers diagram. There might be known results: For a Young diagram, the maximum number of dominoes (maximum tiling) equals the sum over rows of floor(length/2) plus maybe something else? Actually, a domino tiling of a rectangle always exists if area even. But for Young diagram, the shape might have obstacles. Let's think.
The diagram is essentially a set of cells in columns decreasing heights. Dominoes can be placed horizontally (within a row across columns) or vertically (within a column across rows). Since the diagram is left-aligned, rows are continuous from column 1 to some column r_i where row i has length = number of columns with height >= i. Because rows are also left-aligned? Actually the Young diagram is defined by columns: column i has cells in rows 1..a_i (rows counted from bottom). So rows from bottom up: row 1 (lowest) contains cells in columns 1..n? Actually if column n has height >=1, then row 1 includes all columns because a_i >=1? But a_n≥1, so yes row 1 has n cells (since all columns have at least height 1). Row 2: columns with a_i≥2. Since a is non-increasing, row 2 has some prefix of columns: columns 1..k where a_k≥2, a_{k+1}<2. So rows are left-aligned contiguous blocks. So it's a Ferrers diagram. So we have a shape that is both column-convex and row-convex.
Maximum matching in bipartite graph: Since the shape is bipartite, the size of maximum matching equals the size of minimum vertex cover (Kőnig's theorem). However, computing minimum vertex cover for such grid may be doable via some combinatorial optimization.
Alternatively, we can compute maximum domino tiling: Each domino covers one black and one white cell (if we color chessboard). The total number of black and white cells may differ, limiting the matching size: maximum matching cannot exceed min(#black, #white). In a bipartite graph, the maximum matching size = min(|V| - deficiency?), but it's not exactly min(b,w) because there could be structural constraints. Actually in any bipartite graph, the size of maximum matching is at most min(#black, #white). However, there could be a smaller bound due to connectivity, but usually min(b,w) is an upper bound. And if the graph is "bipartite and has a perfect matching on the full grid"? For Ferrers diagram, maybe the deficiency is the difference between number of black and white cells? Let's test with example: a = [3,2,2,2,1]. Compute total cells: sum = 3+2+2+2+1=10. So min(b,w) =? Assuming chessboard coloring with (i,j) cell: row index r, column c. Usually parity: (r+c) mod 2. For the shape, we can compute black and white counts. But let's compute: Row1 (bottom): columns 1..5. Row1: cells (1,1) to (1,5). Let's set coordinates: row 1 bottom, column 1 leftmost. Use (row, column). Usually (r,c) with r from 1 (bottom) to max a_1? Let's compute colors: if (r+c) %2 = 0, say white. Count white vs black. For n=5 columns, rows up to 3. Cells:
Col1: rows1,2,3
Col2: rows1,2
Col3: rows1,2
Col4: rows1,2
Col5: row1
List:
(1,1) -> 1+1=2 even -> white? Actually parity depends on definition. Let's define black as (r+c) mod2 =0 maybe. But it's symmetric: the difference |B-W| is at most 1. For total 10 cells, both colors 5 if even? Actually 10 even, if exactly half? Not necessarily because shape may not have equal counts. In a rectangle with even dimensions, black and white equal. In a shape, they could differ. For example, a single cell: 1 vs 0. For this shape, let's compute: use r from 1 bottom, c from 1 left.
Coordinates:
(1,1): sum=2 even => White.
(1,2): sum=3 odd => Black.
(1,3): sum=4 even => White.
(1,4): sum=5 odd => Black.
(1,5): sum=6 even => White.
So row1: whites: col1,3,5 => 3 whites; blacks: col2,4 => 2 blacks.
Row2: exists columns 1-4? a: col1 height>=2, col2>=2, col3>=2, col4>=2, col5 height=1 so not row2. So row2: cols1-4.
(2,1): sum=3 odd => Black.
(2,2): sum=4 even => White.
(2,3): sum=5 odd => Black.
(2,4): sum=6 even => White.
So row2: whites: col2,4 => 2; blacks: col1,3 =>2. Wait total row2: 4 cells: black: (2,1),(2,3)=2, white: (2,2),(2,4)=2.
Row3: only col1 (since a_1=3). (3,1): sum=4 even => White.
So total whites: row1:3, row2:2, row3:1 =>6? Actually row1 whites=3, row2 whites=2, row3 whites=1 => total whites=6; blacks: row1:2, row2:2 =>4? row3 black=0 => total blacks=4. So total cells 10, white 6, black 4. min=4, but output maximum dominoes=4, which matches min(black,white)=4? Indeed 4 is matching size, which equals the minority color count. In this case, black cells are 4, so matching size = 4? But could there be a case where matching size is less than minority color? Possibly due to structure, but for bipartite graphs the size of maximum matching can be less than min(b,w) if there is no perfect matching covering all minority vertices? Actually min(b,w) is an upper bound, but it's not always attainable. However, for many grid graphs, the deficiency is the difference between black and white cells? No, consider a shape that is like a "checkerboard" with isolated cells. For bipartite graphs, maximum matching size = |V| - maximum independent set? Not that. The maximum matching size equals the size of the minimum vertex cover (Konig). The bound min(b,w) is trivial. It is attained if there is a matching that matches all vertices of the minority side. This is equivalent to the existence of a system of distinct representatives for minority side to majority side via adjacency. That might not always hold due to constraints like Hall's marriage condition. So we need to compute maximum matching for this Ferrers diagram. Perhaps there is a known formula: For a Ferrers diagram, the maximum size of a matching (domino tiling) equals the sum over rows of floor(length_of_row / 2) ??? Let's test: rows: row1 length=5 => floor(5/2)=2; row2 length=4 =>2; row3 length=1 =>0; sum=4. That matches. But is it always? Consider shape where rows are all odd lengths? Example: a = [3,3,3]? That's a 3x3 square, total cells 9, min(b,w)=? 3x3 has 5 and 4? Actually 3x3: black squares? (r+c) parity: 5 of one, 4 of the other. floor(total/2)=4. But sum of floor(row_length/2): each row length 3 => floor(3/2)=1, sum=3. That's less than 4. But can we tile a 3x3 square with dominoes? Maximum dominoes in a 3x3 square: known that maximum is 4? Actually, can we place 4 dominoes in a 3x3? They cover 8 cells, leaving one cell. Is it possible? Let's attempt: cells arranged 3x3. Place two horizontal dominoes covering row1 col1-2 and row1 col2-3? Overlap. Need non-overlapping. Let's try: horizontal dominoes: row1: col1-2, row2: col1-2, row3: col1-2? That would be 3 horizontal dominoes covering columns 1-2 each row, but they'd cover (1,1)(1,2); (2,1)(2,2); (3,1)(3,2) => 6 cells, then can we place a vertical domino somewhere else? Possibly vertical at col3 covering rows1-2? That would cover (1,3)(2,3). So total 4 dominoes, covering 8 cells, leaving (3,3) empty. So yes 4 dominoes possible. But sum floor(row_length/2) gave 3. So that formula undercounts. So not that.
Maybe it's sum over columns of floor(height/2)? For [3,3,3] columns: each height 3 => floor(3/2)=1, sum=3. Also undercounts. So not that.
Perhaps there is known result: For a Young diagram (Ferrers shape), the maximum number of dominoes is equal to the number of cells minus the size of the maximum independent set? Not helpful.
Let's think more systematically. The shape is a bipartite graph where vertices are cells, edges between orthogonal adjacent cells. This is a subgraph of the infinite grid. There is known concept of "rook" or something. But maybe we can compute maximum matching via greedy algorithm? Since shape is monotone, maybe a greedy placement works: process rows from bottom or columns from left, always placing dominoes to cover as many as possible? But need optimal.
Alternative viewpoint: The domino tiling problem is equivalent to a maximum matching in a bipartite graph that is planar and with a certain structure. Could be solved using dynamic programming across columns? n up to 300k, heights up to 300k, so total cells up to sum a_i, which could be up to 9e10? Actually n * max a_i could be 9e10, too large to process individually. So we need a combinatorial formula.
Observation: The Ferrers diagram is a "staircase" shape. There may be known result: The maximum number of dominoes equals the number of cells minus the size of a maximum antichain? Not sure.
Consider the chessboard coloring. For any domino tiling (matching), each domino covers one black and one white. So the number of dominoes is limited by min(#black, #white). However, can we always achieve a matching covering all minority color cells? In a Ferrers diagram, maybe there is a necessary and sufficient condition: For any subshape, the number of black cells should be <= number of white cells plus something? Actually Hall's condition: For any subset S of black cells, the neighborhood (adjacent white cells) must have size at least |S|. Because the graph is bipartite. So to have a matching that matches all black cells (assuming black <= white), we need that for every set of black cells, the set of white cells adjacent to them is at least as large. If this fails, the maximum matching size is less than #black. The deficiency is max over subsets S of (|S| - |N(S)|). So we need to compute the maximum deficiency.
So the maximum matching size = total black - max_{S⊆Black} (|S| - |N(S)|) = total white - max_{T⊆White} (|T| - |N(T)|) but by symmetry.
Thus we need to compute the maximum over subsets of one color of (|S| - |N(S)|). Since shape is special, maybe this maximum is something like the maximum over some "prefix" subsets.
Because the Ferrers diagram adjacency: each cell (r,c) has up to 4 neighbors: up, down, left, right, but only if those cells exist. The bipartite graph: cells alternate colors. Let's fix coloring: say cell (r,c) is black if (r+c) even, white if odd. Or vice versa.
We need to find the largest deficiency. For many grid shapes, the worst-case subset is often a "contiguous" region. Possibly for Ferrers diagram, the extremal S that maximizes deficiency is a set of cells forming a "staircase" shape? There might be a known formula: the maximum number of dominoes equals total cells - |maximum antichain|? That's for rooks? Not.
Another approach: Since the shape is a bipartite graph that is a "grid" with some missing cells at the top right. This is essentially a bipartite graph where rows are on one side and columns on the other? Actually the domino placement corresponds to edges between adjacent cells, but cells themselves are vertices. Could we transform the problem to a matching between rows and columns? Not exactly.
But perhaps there's a simpler solution: The maximum number of dominoes that can be placed in a Young diagram is equal to the number of cells minus the size of a maximum independent set? For bipartite graphs, maximum matching = |V| - size of maximum independent set? No, that's for König's theorem? Actually, for bipartite graphs, α(G) + β(G) = |V| where α = size of maximum independent set, β = size of minimum vertex cover. And ν (matching size) = β (minimum vertex cover) by König. So ν = |V| - α. So if we can compute the size of a maximum independent set (largest set of cells with no two adjacent), then answer = total_cells - α. So find the maximum independent set in this grid graph induced by Ferrers diagram. That might be easier? Because independent set means no two adjacent cells (including diagonally? No, adjacency is orthogonal, so independent set means no two cells share a side). That's a classical problem: maximum independent set on bipartite grid graph is equivalent to maximum set of cells with no edge. That's essentially a maximum "non-attacking" thing. But computing α for large grid may still be complex. However, the grid graph is bipartite and planar, and there might be formula based on parity and shape.
But note: In bipartite graphs, maximum independent set = total vertices - minimum vertex cover. And minimum vertex cover is related to maximum matching, so it's circular.
Alternative: Possibly the answer is simply the minimum between total black and total white? But we saw the 3x3 square: black=5, white=4, min=4, and max dominoes=4, so min works. But is there any Ferrers diagram where max matching < min(b,w)? Let's try small shapes.
Consider a shape that is like a "T" shape: columns: a = [2,1]. Total cells = 3. Coloring: coordinates: (1,1) sum=2 even? (1,1) white, (1,2) black? Actually column1: rows1,2; column2: row1 only. (1,1) white, (2,1) black (since 2+1=3 odd), (1,2) black (1+2=3 odd). So black cells: (2,1),(1,2) => 2, white cells: (1,1) => 1. min=1. Can we place 1 domino? Yes, either vertical domino in column1 covering (1,1)-(2,1) which uses both colors? Actually (1,1) white and (2,1) black, that's valid. So max dominoes = 1 = min(b,w). So min works.
Consider shape: a = [2,2,1]? That's 2+2+1=5 cells. Compute colors: Let's list cells:
Col1: rows1,2
Col2: rows1,2
Col3: row1
Coordinates (r,c):
(1,1): sum=2 even -> white
(2,1): sum=3 odd -> black
(1,2): sum=3 odd -> black
(2,2): sum=4 even -> white
(1,3): sum=4 even -> white? Actually 1+3=4 even -> white.
So whites: (1,1), (2,2), (1,3) =3; blacks: (2,1), (1,2)=2. min=2. Can we place 2 dominoes? Need to cover 4 cells. Try: Place horizontal domino on row1 columns1-2: cells (1,1) white and (1,2) black -> valid. Then we have remaining: (2,1) black, (2,2) white, (1,3) white. Can we place another domino? Options: vertical domino in column1 covering (1,1) used, so not. Vertical in column2 covering (1,2) used. So maybe vertical domino column1? Actually (2,1) black with (1,1) used, not. Could try (2,1)-(2,2) horizontal? That's (2,1) black and (2,2) white, both free, but are they adjacent? Yes, they are adjacent horizontally. So we can place horizontal domino on row2 columns1-2. That uses (2,1) and (2,2). That's two dominoes covering all cells except (1,3). So 2 dominoes possible. So min works.
Consider shape: a = [3,2,1]? total cells=6. Colors: Let's compute:
Col1: rows1,2,3
Col2: rows1,2
Col3: row1
Coordinates:
(1,1): sum2 even -> white
(2,1): sum3 odd -> black
(3,1): sum4 even -> white
(1,2): sum3 odd -> black
(2,2): sum4 even -> white
(1,3): sum4 even -> white? (1+3=4) -> white.
So whites: (1,1),(3,1),(2,2),(1,3) =4; blacks: (2,1),(1,2)=2. min=2. Can we place 2 dominoes? Need cover 4 cells. Try: horizontal row1 col1-2: (1,1) white, (1,2) black -> valid. Then remaining: (2,1) black, (3,1) white, (2,2) white, (1,3) white. Could place vertical domino in column1: (2,1)-(3,1) uses black+white -> valid. That's two dominoes covering 4 cells. So min works.
Consider shape: a = [3,3,2]? That's 8 cells? Actually 3+3+2=8. Colors? Let's compute quickly: Coordinates:
Col1: rows1-3
Col2: rows1-3
Col3: rows1-2
List:
(1,1): 2 even -> W
(2,1): 3 odd -> B
(3,1): 4 even -> W
(1,2): 3 odd -> B
(2,2): 4 even -> W
(3,2): 5 odd -> B
(1,3): 4 even -> W
(2,3): 5 odd -> B
So whites: (1,1),(3,1),(2,2),(1,3) =4? Also maybe (??) Let's list whites: (1,1) W; (2,1) B; (3,1) W; (1,2) B; (2,2) W; (3,2) B; (1,3) W; (2,3) B. So whites: (1,1),(3,1),(2,2),(1,3) => 4 whites. Blacks: (2,1),(1,2),(3,2),(2,3) => 4 blacks. min=4. total cells 8, so max dominoes could be 4 if perfect matching exists. Is it possible to tile entirely with dominoes? Let's try: The shape is essentially a 3x3 rectangle missing the top right cell (since column3 height 2, so cell (3,3) missing). That's a 3x3 with missing top right? Actually coordinates: row3 only columns1 and2 present. So shape is like an L. Can we tile it with 4 dominoes? Need to cover all 8 cells. This is a classic puzzle: Tiling a 3x3 missing a corner? Actually we have all except (3,3). So shape: rows: row1: cols1-3, row2: cols1-3, row3: cols1-2. That's 8 cells. Can we tile with dominoes? Possibly yes. Example: vertical domino col1 rows1-2, vertical col1 rows2-3? Wait col1 rows: 1-2, and 2-3 would overlap. Let's try systematic: Place horizontal domino on row1 cols1-2 (W,B), horizontal on row1 col3? cannot because only one cell col3 row1. So maybe: vertical col1: (1,1)-(2,1) (W,B). vertical col2: (1,2)-(2,2) (B,W). That covers row1,2 cols1-2. Then we have (3,1) W, (2,3) B, (1,3) W, (3,2) B? Actually remaining: row3: (3,1) W, (3,2) B; row2 col3 (2,3) B; row1 col3 (1,3) W. So we have two white and two black. Can we place two dominoes? Possibly (3,1)-(3,2) horizontal: (W,B) valid, and (1,3)-(2,3) vertical: (W,B) valid. Yes, that yields 4 dominoes. So min works.
Now consider shape that is a single column of height 1: n=1, a=[1]. total cells=1, black? Suppose (1,1) sum=2 even white. So min=0, max dominoes=0. OK.
Consider shape with two columns: [2,1]. We did, min=1 works.
Consider shape with [3,1,1]? That's 5 cells. Colors: col1 rows1-3, col2 row1, col3 row1. Coordinates:
(1,1) W, (2,1) B, (3,1) W; (1,2) B; (1,3) W? Actually (1,3) since col3: (1,3) sum=4 even W. So whites: (1,1), (3,1), (1,3) =3; blacks: (2,1),(1,2)=2. min=2. Can we place 2 dominoes? Try: vertical domino col1 rows1-2 (W,B) uses (1,1),(2,1). Then remaining: (3,1) W, (1,2) B, (1,3) W. Can we place another domino? The only possible: horizontal domino (1,2)-(1,3) would be B and W, but are they adjacent? Yes columns2 and3 are adjacent? Since columns are adjacent: column2 and column3 are adjacent horizontally, and both in row1, so (1,2) and (1,3) are adjacent. But (1,2) is black? Actually (1,2) B, (1,3) W, so valid. That uses (1,2),(1,3). So we have two dominoes covering 4 cells, leaving (3,1) white alone. So 2 dominoes possible. min works.
Consider shape: [2,2] (2x2 square). total=4, blacks=2, whites=2, min=2, max dominoes=2. Works.
Consider shape: [4,4,1]? That's 4+4+1=9 cells. Let's compute colors quickly? But maybe min works. So far all examples work with min(b,w). But we need to find a counterexample where min(b,w) is not achievable. For bipartite graphs in general, it's not always achievable. For grid graphs with holes, sometimes deficiency arises. For Ferrers diagram, does Hall's condition always hold for the minority color? In other words, is the Ferrers diagram graph "bipartite and balanced"? Possibly it's a "bipartite permutation graph" or something that has a property that the bipartite adjacency matrix is "convex", ensuring that the graph is "bipartite chain graph"? Let's examine.
The Ferrers diagram shape: columns decreasing. If we view cells as unit squares, adjacency edges are between orthogonal neighbors. The bipartite graph: vertices are cells, edges between cells sharing a side. This graph is planar and bipartite. There is known concept of "grid graph" and "rook's graph" but not.
But perhaps we can prove that the maximum matching size equals the number of cells minus the size of a maximum independent set, and that maximum independent set equals something like sum of ceil(row_length/2) or something? Let's test on 3x3 square: total=9, max independent set? In a 3x3 grid, the maximum set of cells with no two adjacent? That's like a chessboard: maximum independent set is 5 (take all squares of one color). Indeed in bipartite graph, the maximum independent set is total - minimum vertex cover, and min vertex cover = matching size = 4, so α = 9-4=5. That matches the larger color class. In a Ferrers diagram, the two color classes sizes can be computed. The deficiency (black - something) might be the absolute difference between black and white? Actually α = max(#black, #white) because one can take all vertices of one color to form an independent set (since no two same-colored cells are adjacent). That's always an independent set because adjacency only connects opposite colors. So the larger color class is an independent set. So α ≥ max(#black, #white). Moreover, α ≤ total vertices - ν. Since ν ≤ min(#black, #white), we have α ≥ max(#black,#white) and α = total - ν. If ν = min(b,w), then α = total - min(b,w) = max(b,w). So indeed if the maximum independent set equals the larger color class, then ν = min(b,w). Conversely, if there exists a larger independent set than the larger color class, then ν < min(b,w). But can there be an independent set larger than the larger color class? In a bipartite graph, any independent set can be partitioned into some black and some white cells, but the maximum independent set is total vertices minus minimum vertex cover. Since the minimum vertex cover is at least min(b,w)?? Actually ν = min vertex cover size, and ν ≤ min(b,w) because one can take all vertices of the minority color as a vertex cover? Check: Taking all black vertices covers all edges? Not necessarily; edges might be between white and white? No, edges only between black and white, so if you take all black vertices, you cover all edges because every edge is incident to a black vertex? But is every edge incident to a black vertex? In a bipartite graph with bipartition (B,W), each edge has one endpoint in B and one in W. So covering all B vertices covers all edges. So taking the entire minority color class (if B is smaller) is a vertex cover. So the minimum vertex cover size ≤ min(|B|,|W|). Hence ν = min vertex cover ≤ min(|B|,|W|). Also, by König, maximum matching = ν. So ν ≤ min(|B|,|W|). Equality ν = min(|B|,|W|) if and only if the minority color class itself is a minimum vertex cover, i.e., if every edge is incident to a minority vertex? Actually taking all minority vertices is a vertex cover, but it might not be minimum; there could be a smaller cover. So ν = min(|B|,|W|) if and only if there exists a vertex cover of size min(|B|,|W|). Since minority class itself is a vertex cover of size min(|B|,|W|), that set is a vertex cover. But is it always a vertex cover? Yes, because any edge connects a black and a white; if we take all vertices of the smaller color class, then every edge has at least one endpoint in that set? Not necessarily: If the edge is between a black vertex and a white vertex, if we take all black vertices, then edges with black endpoint are covered; but if we take all white vertices (if white is smaller) then edges are covered. However, if we take the smaller class, does it cover all edges? For an edge between a black and a white, if we take the smaller class, say black class is smaller; then that edge's black endpoint is in the cover, so it's covered. If we take white class because it's smaller, then white endpoint is covered. So indeed, whichever class is smaller, that class is a vertex cover. Because every edge has one black and one white; the smaller class includes all vertices of that color, so every edge is incident to a vertex of that color, thus covered. So the smaller color class is always a vertex cover! Wait, careful: The smaller class includes all vertices of that color? If we take the set of all black vertices, we cover all edges because every edge has a black endpoint. That is true regardless of whether black is smaller or larger. Actually, taking all black vertices is a vertex cover because each edge has a black endpoint. The size of that cover is |B|. Similarly, taking all white vertices gives cover size |W|. So the minimum vertex cover size is at most min(|B|,|W|). But is the set of all black vertices always a vertex cover? Yes, because every edge connects a black and a white, so it has a black endpoint. So indeed, any bipartite graph with bipartition (B,W) has vertex cover B, and also vertex cover W. Therefore, the minimum vertex cover size ≤ min(|B|,|W|). And by König's theorem, maximum matching size = min vertex cover size. Therefore, maximum matching size ≤ min(|B|,|W|). And we can achieve min(|B|,|W|) if we can find a matching that matches every vertex of the smaller color class. But that's not guaranteed; the existence of a matching that covers the smaller side is equivalent to Hall's condition for that side. However, the bound min(|B|,|W|) is an upper bound, and we have a vertex cover of size min(|B|,|W|) (the smaller color class). So the minimum vertex cover could be smaller than min(|B|,|W|) if there is a cover with even fewer vertices (some mix of both colors). But then the maximum matching size would be that smaller number. But we also have that the smaller color class itself is a vertex cover, so the minimum vertex cover size is at most min(|B|,|W|). It could be strictly less. In that case, max matching size = that smaller number, which is less than min(|B|,|W|). However, does there exist a Ferrers diagram where the smaller color class is not a minimum vertex cover, i.e., there exists a vertex cover with size < min(|B|,|W|)? That would require that some vertices of the smaller color class are not essential; we can cover all edges without including all of them. Equivalent to existence of a matching of size less than min(|B|,|W|) but equal to something else. But the smaller class is a vertex cover; if we can find a cover of size < |B| (if B is smaller), then that cover must consist of some mixture, meaning that there are edges not incident to some black vertices, so those black vertices are isolated? Not exactly. Let's analyze: Suppose black is the smaller side. If there exists a vertex cover C with |C| < |B|, then C cannot contain all black vertices. There exists at least one black vertex v not in C. Since C is a cover, all edges incident to v must have their other endpoint in C. That is, all neighbors of v are in C. Also, because v is black, its neighbors are white. So if we can find a set of white vertices that cover all edges incident to black vertices not in the cover, we might reduce the size. But we need to cover edges from all black vertices, not just the ones not in C. So essentially, a vertex cover of size less than |B| would need to "replace" some black vertices with some white vertices, covering edges more efficiently. In bipartite graphs, the size of minimum vertex cover equals the size of maximum matching. So to have ν < min(|B|,|W|), we need that the maximum matching is less than min(|B|,|W|). That happens when Hall's condition fails for the smaller side: there exists a subset S of the smaller side such that |N(S)| < |S|. In that case, the maximum matching size = |B| - δ where δ is the deficiency. And the minimum vertex cover size would be |B| - δ (if B is smaller) but that's less than |B|. So the existence of a Hall-deficient subset of the smaller side reduces the maximum matching below |B|. So we need to check whether in Ferrers diagrams, there can be a subset S of (say) black cells with |N(S)| < |S|. If such S exists, then min(b,w) is not achievable.
Let's attempt to construct a Ferrers diagram where Hall fails for the minority color. Since the shape is monotone, maybe it's always satisfied? But earlier we considered a 3x3 square: b=5, w=4, minority=4, and we achieved matching 4, so Hall holds for white side? Actually white side is smaller? In 3x3, white=4, black=5. So minority=white=4. Does there exist a subset of white cells with fewer neighbors? Let's test: pick all white cells. Their neighbors: each white cell has up to 4 neighbors (within shape). In 3x3, the white cells positions? Choose coloring: (1,1) maybe white? Actually earlier we had (1,1) white? Let's standardize: Let's define parity such that (1,1) is black? Typically chessboard: (1,1) black. But regardless, the specific assignment doesn't matter; one color has 5, the other 4. For the minority (size 4), we need to check if there is a subset S of minority with |N(S)| < |S|. The worst might be S = all minority. Their neighbors would be the set of adjacent cells to those whites. In a full grid, each interior cell has degree 4, boundary less. Could the neighbor set be smaller? Possibly if the minority cells are all on the boundary and their neighbors are overlapping. For a full 3x3, if we take all 4 whites, their neighbors might be all 5 blacks, so |N(S)|=5 >=4. So Hall holds. But is there any Ferrers shape where a subset of minority color has insufficient neighbors? Let's try a shape that is like a "staircase" with isolated cells. Perhaps shape: a = [3,1]? That's columns: col1 height 3, col2 height 1. Total cells=4. Colors: coordinates: (1,1) parity? Let's set (r,c) with r from 1 bottom, c from 1 left. Use (r,c) sum mod2. (1,1): 2 even -> white; (2,1):3 odd -> black; (3,1):4 even -> white; (1,2):3 odd -> black. So whites: (1,1),(3,1) ->2; blacks: (2,1),(1,2) ->2. min=2. Can we achieve 2 dominoes? Shape: cells: column1: three stacked, column2: one at bottom. This shape is like an L. Can we tile with two dominoes? We need cover all 4 cells. Try vertical domino in column1 covering (1,1)-(2,1) [white-black], then left (3,1) white and (1,2) black. They are not adjacent ( (3,1) is above, (1,2) is to the right but not adjacent vertically or horizontally: (3,1) neighbor down is (2,1) used, up none, left none, right no cell at (3,2). (1,2) neighbor left (1,1) used, down none, up none. So no domino possible. Could we do horizontal domino (1,1)-(1,2) white-black, then vertical domino (2,1)-(3,1) black-white. That works! Horizontal covers (1,1) and (1,2); vertical covers (2,1) and (3,1). So 2 dominoes possible. So min works.
Try shape: a = [4,1]? columns: col1 height4, col2 height1. Total=5 cells. Colors: (1,1) white, (2,1) black, (3,1) white, (4,1) black? Actually parity: (1,1) white (2), (2,1) black (3), (3,1) white (4), (4,1) black (5), (1,2) black (3). So whites: (1,1),(3,1) =2; blacks: (2,1),(4,1),(1,2)=3. minority=2. Can we achieve 2 dominoes? We need cover 4 cells, leaving one. Try horizontal (1,1)-(1,2) white-black, then vertical (2,1)-(3,1) black-white, then left (4,1) black alone? That's 2 dominoes covering 4 cells, works. So min works.
Now try shape: a = [4,2]? columns: col1 height4, col2 height2. Total cells=6. Colors: compute:
(1,1) W
(2,1) B
(3,1) W
(4,1) B
(1,2) B (1+2=3)
(2,2) W (2+2=4)
So whites: (1,1),(3,1),(2,2) =>3; blacks: (2,1),(4,1),(1,2) =>3. equal. So min=3. Can we tile all 6 cells with 3 dominoes? Possibly. Try: horizontal (1,1)-(1,2) W-B, vertical (2,1)-(3,1) B-W, vertical (4,1)?? can't. Let's attempt systematically. The shape is 4x2 but missing top of column2 above row2. Actually column2 has rows1-2. So cells: (1,1),(2,1),(3,1),(4,1) in col1; (1,2),(2,2) in col2. We need cover all cells with dominoes. This is like a 4x2 rectangle with the top two cells of column2 missing. Can we tile? Possibly: vertical domino in col1 covering (1,1)-(2,1) and (3,1)-(4,1) as two vertical dominoes? Actually vertical dominoes in col1: (1,1)-(2,1) (W-B) and (3,1)-(4,1) (W-B? (3,1) W, (4,1) B) -> that uses all col1 cells. Then we have (1,2) B and (2,2) W left, can they form a domino? They are adjacent vertically? (1,2) and (2,2) are adjacent (vertical), yes. That would be B-W. So three dominoes: vertical (1,1)-(2,1), vertical (3,1)-(4,1), vertical (1,2)-(2,2). That's valid. So 3 dominoes, perfect tiling. So min works.
Try shape: a = [5,1]? total=6. Colors: (1,1)W,(2,1)B,(3,1)W,(4,1)B,(5,1)W? Actually (5,1): 5+1=6 even ->W; (1,2) B. So whites: (1,1),(3,1),(5,1) =>3; blacks: (2,1),(4,1),(1,2)=>3. min=3. Can we tile all 6? Horizontal (1,1)-(1,2) W-B; vertical (2,1)-(3,1) B-W; vertical (4,1)-(5,1) B-W. Works. So min works.
Thus far min(b,w) seems achievable. But we need to be careful: Is it always true for Ferrers diagrams that the minority color class is a minimum vertex cover? That would mean that the maximum matching equals min(b,w). Is that always true? Let's test a more complex shape where perhaps Hall's condition fails. Consider a shape that is "thin" with many isolated cells? But adjacency is only orthogonal, so some cells might have degree 1. Could a subset of minority color cells all be adjacent to the same white cell, causing deficiency? For minority color class (say black), if we have two black cells both adjacent only to the same white cell, then for S={those two black}, N(S) could be 1 <2, causing deficiency. Can that happen in a Ferrers diagram? Let's see if such configuration is possible.
We need two black cells that share a common neighbor white cell. Since adjacency: a black cell is adjacent to up to 4 white cells (north, south, east, west). Two black cells sharing a white neighbor would mean that white cell is adjacent to both black cells. That implies that the white cell is adjacent to two black cells that are either opposite directions (north and south) or (east and west) or diagonal? Actually if white cell at (r,c), its neighbors are (r-1,c), (r+1,c), (r,c-1), (r,c+1). For two black cells both to be adjacent to this white, they must be two of these positions, but they would both be black only if the parity of those positions are the same color (black). Since (r,c) is white, its neighbors have opposite color, i.e., black. So all four neighbors of a white cell are black. So if a white cell has two black neighbors, they are distinct and both black. So it's possible that two black cells both have only that white cell as their only neighbor if they are isolated? But in a Ferrers diagram, if a cell is interior, it typically has up to 4 neighbors. For a black cell to have only one neighbor, it must be at a corner or isolated. But can two black cells both have degree 1 and both connect to the same white cell? That would require the white cell to be adjacent to both black cells, and those black cells have no other neighbors. For example, consider shape:
Cells: white at (1,1) (say), black at (1,2) and (2,1) both adjacent to (1,1). But (1,2) might also have other neighbors: (1,3) if exists, (2,2), (1,1). For it to have only neighbor (1,1), we need that (1,3) and (2,2) are not present. Similarly, (2,1) would need (2,0) (none), (3,1) and (2,2) not present. So we need a shape where at a corner, we have a white cell with two black cells adjacent, and those black cells are "dangling". But Ferrers shape is left-justified and columns non-increasing, so the shape includes all cells below and to the left of the boundary. Let's attempt to create a shape with a "hole" or missing cells that cause such isolation. However, Ferrers diagram is "convex" in the sense that if a cell (r,c) is present, then all cells with lower row and same column (i.e., below) are present (since column is continuous from bottom), and all cells with same row and lower column (to the left) are present (since rows are left-justified). So it's a Young diagram: it consists of cells such that if (r,c) is in diagram, then for any r' ≤ r, (r',c) is in diagram (because column continuous), and for any c' ≤ c, (r,c') is in diagram (because row continuous from left). So the shape is a "down-right" closed set: it's an order ideal in the product poset. This property ensures that there are no holes.
Given that, can we have a configuration where two black cells both have only one neighbor, and that neighbor is the same white cell? Let's try to construct.
We need a white cell W at (r,c). Its four neighbors: (r-1,c), (r+1,c), (r,c-1), (r,c+1). For the diagram to be Ferrers, presence of these neighbors depends. For W to be present, we need column c has height at least r, and row r has length at least c (since row r includes columns up to some max). For neighbor (r-1,c) to be present, need r-1 ≥1 and column c height ≥ r-1, which is true if column c height ≥ r (since it's continuous from bottom). So (r-1,c) will be present provided r>1. Similarly, (r+1,c) present if column c height ≥ r+1. (r,c-1) present if c>1 and row r length ≥ c-1 (i.e., column c-1 height ≥ r). (r,c+1) present if c < n and row r length ≥ c+1 (i.e., column c+1 height ≥ r). So typically interior white cell will have all four neighbors if the shape extends in all directions. But we want some black neighbors to have degree 1. For black neighbor (r-1,c) to have degree 1, it must have no other neighbors besides W. Its neighbors: (r-2,c), (r-1,c-1), (r-1,c+1), and (r,c) (which is W). For it to have only W, we need that (r-2,c) absent (i.e., column c height < r-1? Actually if column c height = r-1 exactly, then (r-2,c) present? Wait column c height = a_c. If a_c = r-1, then column c has rows 1..r-1. So (r-2,c) would be present if r-2 ≥ 1, so yes. So to have (r-2,c) absent, we need r-2 < 1 or column c height < r-1? Actually if column c height = r-1, then rows 1..r-1 exist, so (r-2,c) exists as long as r-2 ≥1. So to make (r-2,c) absent, we need column c height < r-1, i.e., a_c ≤ r-2. But then (r-1,c) itself might not exist because column c height must be at least r-1 to have (r-1,c). So we need a_c = r-1 to have (r-1,c) exist. But then (r-2,c) exists if r-2 ≥1, which is typical. So cannot have (r-1,c) with no other neighbor above. Similarly, for (r-1,c) to have no left neighbor (r-1,c-1), we need either c=1 or row r-1 length < c-1 (i.e., column c-1 height < r-1). Since shape is left-justified, if column c-1 exists, its height is at least a_c? Actually a_{c-1} ≥ a_c because non-increasing. So if a_c ≥ r-1, then a_{c-1} ≥ r-1, so column c-1 also has row r-1. So (r-1,c-1) exists if c>1. So that neighbor exists. So (r-1,c) would have left neighbor. So degree >1. So it's unlikely that a black cell has degree 1 unless it's on the boundary of shape, but even then it may have multiple neighbors.
Let's analyze degree of a cell in a Ferrers diagram.
- A cell at (r,c) (with 1 ≤ r ≤ a_c, and since columns non-increasing, c ≤ number of columns with height ≥ r) has neighbors:
- Up: (r+1,c) exists iff r+1 ≤ a_c.
- Down: (r-1,c) exists iff r>1.
- Left: (r,c-1) exists iff c>1 and a_{c-1} ≥ r (since row r exists in column c-1 if column c-1 height ≥ r). Because rows are left-justified: row r includes columns 1..k where k = max{j : a_j ≥ r}. So if c>1, then column c-1 must have height ≥ r because otherwise row r would not extend to column c? Actually if a_{c-1} < r, then column c-1 does not have row r, but cell (r,c) could still exist. For (r,c) to have a left neighbor (r,c-1), we need that cell (r,c-1) exists, which requires a_{c-1} ≥ r. Since columns are non-increasing, a_{c-1} ≥ a_c, but it's possible that a_{c-1} = a_c? Actually a_{c-1} ≥ a_c. So if a_c ≥ r, it does not guarantee a_{c-1} ≥ r; a_{c-1} could be less than r? Wait a_{c-1} ≥ a_c. So if a_c ≥ r, then a_{c-1} ≥ a_c ≥ r, so a_{c-1} ≥ r. So if (r,c) exists, then a_c ≥ r. Since a_{c-1} ≥ a_c ≥ r, we have a_{c-1} ≥ r, so (r,c-1) exists for c>1. Indeed, because the Ferrers condition: columns are non-increasing, so left columns are at least as tall. So any cell (r,c) with c>1 automatically has a left neighbor. Similarly, does it guarantee a right neighbor? Not necessarily, because column c+1 may be shorter; (r,c+1) exists iff a_{c+1} ≥ r. Since a_{c+1} ≤ a_c, it's possible that a_{c+1} < r, so right neighbor may be missing. So each cell always has left neighbor if not first column, and down neighbor if not bottom row, because r>1 implies (r-1,c) exists (since a_c ≥ r > r-1). Up neighbor exists only if r < a_c. Right neighbor exists only if a_{c+1} ≥ r.
Thus, cells in the interior (not on top or right boundary) have at least left and down neighbors (and possibly up/right). So degree at least 2 for most cells. A cell on the leftmost column (c=1) has no left neighbor, but always has down neighbor (if r>1). Up neighbor if not top. So could have degree 1 only if it's at the bottom of a column? Actually bottom cell (r=1) has no down neighbor. But may have left? c=1 no left; right neighbor depends; up if column height >1. So bottom-left cell (1,1) has only right neighbor if a_2 ≥ 1 (always, since a_2 ≥ 1 but could be 0?