In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place.
To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v.
Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B).
For example, if n=3 and there are 3 groups X, Y and Z: X = {1, 5, 6}, Y = {2, 4, 9} and Z = {3, 7, 8}. In this case, the values of f are equal to:
f(X,Y)=4 because of 5 → 2, 5 → 4, 6 → 2, 6 → 4,
f(X,Z)=2 because of 5 → 3, 6 → 3,
f(Y,X)=5 because of 2 → 1, 4 → 1, 9 → 1, 9 → 5, 9 → 6,
f(Z,Y)=5 because of 3 → 2, 7 → 2, 7 → 4, 8 → 2, 8 → 4.
Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A ≠ B) is maximal.
In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A ≠ B) as big as possible.
Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division.
If there are many optimal divisions, you can find any.
Input
The only line contains one number n (2 ≤ n ≤ 300).
Output
Output n lines:
In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want.
If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any.
Example
Input
3
Output
2 8 5
9 3 4
7 6 1
Note
In the first test we can divide 9 labs into groups {2, 8, 5}, {9, 3, 4}, {7, 6, 1}.
From the first group to the second group we can transport 4 units of water (8 → 3, 8 → 4, 5 → 3, 5 → 4).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
From the first group to the third group we can transport 5 units of water (2 → 1, 8 → 7, 8 → 6, 8 → 1, 5 → 1).
From the second group to the first group we can transport 5 units of water (9 → 2, 9 → 8, 9 → 5, 3 → 2, 4 → 2).
From the second group to the third group we can transport 5 units of water (9 → 7, 9 → 6, 9 → 1, 3 → 1, 4 → 1).
From the third group to the first group we can transport 4 units of water (7 → 2, 7 → 5, 6 → 2, 6 → 5).
From the third group to the second group we can transport 4 units of water (7 → 3, 7 → 4, 6 → 3, 6 → 4).
The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division.
We need to solve: given n^2 numbers 1..n^2 sorted by height (lowest 1, highest n^2). We need partition into n groups of size n to maximize the minimum f(A,B) over unordered pairs A≠B (or directed pairs? The problem statement: "min f(A,B) over all possible pairs of groups A and B (A ≠ B)" – that suggests both ordered pairs? Actually they say "minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A ≠ B) as big as possible." That suggests we consider directed pairs (A,B) with A≠B. In example they list both directions. The min over all such directed pairs should be maximal. So we need to maximize the minimum directed flow between any two distinct groups (in one direction). So it's a directed quantity: for any ordered pair (i,j) with i≠j, f(G_i, G_j) should be as large as possible, and we maximize the smallest such value.
We need to output any partition achieving optimum.
Constraints: n up to 300, so n^2 up to 90,000. Need algorithm.
We need to think combinatorially about these flows. The total number of directed pairs (ordered) between groups: n*(n-1). For each unordered pair (A,B), we have two directed flows: f(A,B) and f(B,A). They are not symmetric generally.
We can think of the groups as a partition of the numbers. The flow from group A to group B: count of pairs (u,v) with u∈A, v∈B, u > v. That is the number of inversions from A to B: for each pair (u∈A, v∈B), if u > v then contributes 1. Equivalent to: if we treat A as set of numbers, B as set, then f(A,B) = |{(a,b): a∈A, b∈B, a>b}|.
We can also express in terms of total pairs between A and B: |A||B| = n^2. Then f(A,B) + f(B,A) = total number of ordered pairs (a,b) where a≠b? Actually for any unordered pair {a∈A, b∈B}, exactly one of a>b or b>a holds, except when a=b but they are from different groups so they are distinct numbers; always a≠b. So for each unordered pair (a,b) with a∈A, b∈B, exactly one of the two orders yields a>b. So f(A,B)+f(B,A) = |A||B| = n^2. Indeed if a>b then a contributes to f(A,B) if a∈A, b∈B; else if b>a then contributes to f(B,A). So we have f(A,B) + f(B,A) = n^2. Thus the two directed flows are complementary: f(B,A) = n^2 - f(A,B). So the minimum over directed pairs is essentially the minimum of f(A,B) over all ordered pairs (A,B) with A≠B. Because if we take the min over both directions, then if for some unordered pair, f(A,B) is small, then f(B,A) is large (= n^2 - small). But the min will be the small one. So the problem is equivalent to: partition into n groups size n to maximize the minimum over unordered pairs (A,B) of min( f(A,B), f(B,A) )? Actually the min over directed pairs is min( min over unordered {A,B} of f(A,B), f(B,A) ) = min over unordered of min(f(A,B), f(B,A)). Because if for a given unordered pair one direction is small, the min across all directed is that small value. So maximizing the minimum directed flow is equivalent to ensuring that for every unordered pair of groups, both f(A,B) and f(B,A) are as large as possible? Not exactly; we only care about the smaller of the two for each pair, and we want the worst-case smaller value to be as high as possible. So we want to maximize the minimum over unordered pairs of the smaller of the two directed flows. Since they sum to n^2, the smaller is at most floor(n^2/2). So optimum cannot exceed floor(n^2/2). But we might not be able to achieve that high.
Observation: For any two groups, f(A,B) = sum_{a∈A} ( number of b∈B with b < a ) = sum_{a∈A} rank among B. Also can be expressed via ordering of all elements.
We need to design groups to make the directed flows balanced.
Simplify: Represent the numbers 1..n^2 as sorted ascending. We can think of "heights": low numbers are low altitude. Water flows from higher number to lower number (since u>v means water flows from lab u (higher) to lab v (lower)). So higher numbers are at higher altitude. So f(A,B) is number of ways water can go from labs in A to labs in B: i.e., from A to B if A contains higher numbers relative to B.
If group A has overall high numbers, then for any other group B, f(A,B) will be large if B has low numbers, but f(B,A) will be small. So the min of the two directions would be small. So to maximize the minimum, we want groups to be as balanced as possible in terms of the heights of their members. That is, each group should have a mix of low, medium, high numbers, such that for any two groups, the number of pairs where a higher number in one group and lower in the other is roughly half of n^2.
This resembles constructing "orthogonal" groups like Latin squares? For example, the sample output for n=3: groups are {2,8,5}, {9,3,4}, {7,6,1}. Sorted: group1: {2,5,8}, group2: {3,4,9}, group3: {1,6,7}. The sets are somewhat interleaved.
Maybe we can design groups by arranging numbers in an n x n grid sorted by rows? For instance, put numbers 1..n^2 in a matrix row-wise: row i has numbers (i-1)n+1 .. in. Then each column could be a group? Let's test: n=3, columns: groups: {1,4,7}, {2,5,8}, {3,6,9}. Compute f between groups? Let's calculate directed flows: between group1 {1,4,7} and group2 {2,5,8}. f(1,2): count a∈1, b∈2, a>b. a=1 >b? none; 4>2? yes; 4>5? no; 7>2? yes; 7>5? yes; 7>8? no. So total: 4>2,7>2,7>5 => 3. f(2,1): n^2 - f(1,2) =9-3=6. min=3. Similarly group1 vs group3 {3,6,9}: f(1,3): 4>3, 7>3,7>6 =>3; f(3,1)=6 min=3. group2 vs group3: f(2,3): 5>3,8>3,8>6 =>3; f(3,2)=6 min=3. So min directed flow =3. But optimum from example is 4. So 4 >3. So column partition not optimal.
What about rows? Groups: {1,2,3}, {4,5,6}, {7,8,9}. Then f(1,2): group1 to group2: 1,2,3 vs 4,5,6. Since all numbers in group1 < group2, f(1,2)=0 (since no a> b). So min=0 obviously.
So we need better.
The example groups are like each group gets one from each "level"? Perhaps the optimal grouping is to assign numbers in a "balanced" way: sort numbers and assign them to groups in a pattern: like a Latin square where each group gets exactly one element from each quantile. For instance, we can assign numbers 1..n^2 in increasing order to positions in an n x n matrix, then take rows or columns or something? But we need groups of size n. Perhaps we can arrange the numbers in an n x n grid such that each row corresponds to a group? Let's attempt: For n=3, optimal output given: group1: 2,8,5 (not sorted). Sorted: 2,5,8; group2: 3,4,9; group3: 1,6,7. If we sort groups by their elements, we see that each group has one low (1-3), one mid (4-6), one high (7-9). Indeed: group1: 2 (low),5(mid),8(high); group2:3(low),4(mid),9(high); group3:1(low),6(mid),7(high). So each group has one element from each third. Moreover, the low elements are {1,2,3} each in different groups, mids {4,5,6} each in different groups, highs {7,8,9} each in different groups. So it's a partition where each group picks exactly one from each of n "blocks" of size n (blocks: 1..n, n+1..2n, ..., (n-1)n+1..n^2). So it's a transversal of the blocks. That resembles a Latin square: each block is a set of n numbers, and we want to assign each block's numbers to the n groups, one per group. That is essentially constructing n permutations: for i from 0 to n-1 (block i: numbers i*n+1 to (i+1)*n), we assign these n numbers to the n groups, each group gets exactly one number from each block. Then each group ends up with n numbers, one from each block. This is a common construction for such balancing problems.
Let's test for n=3: blocks: [1,2,3], [4,5,6], [7,8,9]. If we assign block1 to groups as: group1 gets 2, group2 gets 3, group3 gets 1; block2: group1 gets 5, group2 gets 4, group3 gets 6; block3: group1 gets 8, group2 gets 9, group3 gets 7. That's exactly the example (order within groups arbitrary). So indeed it's that.
Now we need to see if such a construction yields optimal min f. For n=3, it gave min=4. Let's check other possible assignments? Could we get min=5? The maximum possible min f is at most floor(n^2/2)=4.5 => floor 4? Actually n^2=9, half is 4.5, so the directed flow cannot exceed 4.5, but it's integer, so max possible min is 4? Because if min were 5, then for each unordered pair, the smaller directed flow ≥5, then the larger ≤4 (since sum=9). That would force both ≥5 and ≤4 simultaneously, impossible unless n^2 is even. For odd n^2, the maximum possible smaller flow is floor(n^2/2) = 4. So 4 is optimal for n=3. So the construction achieves the upper bound. For even n^2 (i.e., n even), n^2 is even, so maximum possible smaller flow is n^2/2. For n=2: n^2=4, max min =2? Let's see if we can achieve min=2. Groups of size 2. Suppose we try the transversal construction: blocks: [1,2], [3,4]. We assign each group one from block1 and one from block2. For example group1: {1,4}, group2: {2,3}. Then compute f(1,2): a∈1, b∈2: 1>2? no; 1>3? no; 4>2? yes; 4>3? yes =>2; f(2,1)=4-2=2 => min=2. So yes. Could we get min=2? Upper bound is 2. So works. For n=4: n^2=16, max possible min =8? But is it achievable? Let's test with transversal construction? We need to check if we can achieve min = n^2/2 for even n.
But we need to be careful: The directed flow from A to B: given both groups have one element from each block (size n). Let's denote blocks: B_1 (lowest), B_2, ..., B_n (highest). For group A, let a_i be the element from block i (but order within block: each block contains numbers from (i-1)n+1 to i n. However, within a block, the numbers have ordering but they are contiguous. If each group picks exactly one from each block, then the sets are "balanced": they each contain exactly one number from each of the n intervals of length n. Then for any two groups A and B, we can compute f(A,B). Let's denote the elements: for block k, group A gets A_k, group B gets B_k. These are numbers within the same interval [ (k-1)n+1, k n ]. Since they are distinct (different groups get different numbers from same block), we have A_k and B_k are two distinct numbers from that block. The contribution to f(A,B) from cross-block pairs: For a block i (A_i) and block j (B_j) with i>j, then because all numbers in block i are larger than all numbers in block j (since blocks are intervals and i>j), we have A_i > B_j always. Similarly, if i<j, then A_i < B_j always. For i=j (same block), then we compare A_i and B_i: one may be larger than the other, but both are within the same block, so it's not always true that one is larger than all from the other block? Actually for the same block, A_i and B_i are two numbers in the same interval; we need to count pair (A_i, B_i) as well? Because we consider all pairs (a∈A, b∈B). So for block i, we have a pair (A_i, B_i). Also we have pairs across blocks: (A_i, B_j) for i≠j.
Thus total f(A,B) = sum_{i=1..n} sum_{j=1..n} indicator(A_i > B_j). Since for i>j, always A_i > B_j because block i entirely > block j. For i<j, always A_i < B_j. For i=j, depends on which of A_i and B_i is larger.
Let x_i = 1 if A_i > B_i, else 0. Then number of ordered pairs where i>j contributes: For each i>j, there is exactly 1 pair (since each block gives one element per group). Number of such i>j pairs is number of pairs (i,j) with i>j = C(n,2) = n(n-1)/2. For i<j, contributes 0. For i=j, contributes x_i.
So f(A,B) = n(n-1)/2 + sum_{i=1}^n x_i.
Similarly, f(B,A) = n(n-1)/2 + sum_{i=1}^n (1 - x_i) = n(n-1)/2 + n - sum x_i = n(n+1)/2 - sum x_i.
Because sum over i of (1 - x_i) = n - sum x_i.
Thus the two directed flows are: f(A,B) = T + S, f(B,A) = T + (n - S), where T = n(n-1)/2, S = sum x_i, with S ∈ [0, n], integer.
Hence f(A,B) can range from T to T+n, and f(B,A) from T to T+n, with sum = 2T + n = n(n-1) + n = n^2, consistent.
Now the smaller of the two for this pair is min(T+S, T + n - S) = T + min(S, n - S). Since T is fixed, the min is determined by S, which is the number of blocks where A's element is greater than B's element.
Thus min_f = T + min(S, n - S). Since S is integer from 0..n, the minimal possible value for this pair is T when S=0 or S=n, and maximal when S = floor(n/2) or ceil(n/2) giving min = T + floor(n/2) if n even? Actually min(S, n-S) maximum is floor(n/2). So the best we can do for a given pair is to have S as close to n/2 as possible, making min = T + floor(n/2). The worst-case across pairs would be the minimum over all unordered pairs of (T + min(S_AB, n - S_AB)). Our goal is to maximize this worst-case min. So we need to assign numbers within each block to groups such that for any two groups A,B, the number of blocks where A's element > B's element, i.e., S_AB, is as balanced as possible: i.e., min(S_AB, n - S_AB) as large as possible, ideally floor(n/2). But we also have constraints: within each block, we assign the n distinct numbers to n groups. This is equivalent to constructing n permutations? Actually, we have n blocks. For each block k (which is a set of n numbers), we assign them to the n groups (group indices 1..n) according to a bijection. That is, we have for each block k a permutation π_k of group indices, mapping group i to the number from block k assigned to group i? Or more conveniently, we define for each group an n-length vector of numbers, each from distinct blocks. So the assignment can be represented by an n x n matrix M where rows correspond to groups, columns correspond to blocks (ordered low to high). Entry M[i][k] is the number from block k assigned to group i. Within each column (block), the entries are a permutation of the numbers in that block.
Now for two groups i and j, S_ij = number of columns k where M[i][k] > M[j][k]. Since within each column, the two numbers are comparable (they are distinct numbers from the same block). So S_ij counts how many columns where row i's value > row j's value.
Thus the problem reduces to: given an n x n matrix, each column is a permutation of the numbers in some block, but the actual numeric values within a block are consecutive and increasing: block k numbers are (k-1)n+1, (k-1)n+2, ..., (k-1)n+n. However, the relative ordering within a column is essentially determined by which group gets the larger number; since numbers are distinct, one group gets a larger number than the other in each column. So if we assign numbers arbitrarily but respecting that each column is a permutation, then for each column k, the ordering among groups is determined by the ranking of the assigned numbers. But since the numbers are consecutive, the ordering is essentially any total order; we can decide which group gets the maximum, which gets the second, etc. Actually we can assign the values arbitrarily, but the condition that the numbers are specific consecutive numbers imposes that not all relative orders are independent across columns? Wait, within a column, we have n distinct numbers; we assign them to n groups. This assignment defines a permutation (ranking) of groups according to the values assigned: the group that receives the highest number in that column is "top" for that column, etc. But for pairwise comparisons, we only need to know for each column which of the two groups gets the larger number. So effectively, for each column k, we have a total order of groups, and for any pair (i,j), we can tell who is higher in that column. So S_ij is the number of columns where i > j (i's number larger than j's).
Thus the problem becomes: design n permutations (for n columns) of the set [n] (group indices) such that for every distinct i,j, the number of columns where i precedes j (or i > j) is as balanced as possible, i.e., min(count_i>j, count_j>i) is maximized. Since total columns = n, the count where i > j plus count where j > i equals n (since in each column, exactly one of i>j or j>i holds, as numbers are distinct). So for each pair (i,j), we have S_ij = number of columns where i > j, and n - S_ij = number where j > i. Then the min for that pair is min(S_ij, n - S_ij). So we want to maximize the minimum over all pairs of min(S_ij, n - S_ij). Since S_ij is integer, the best possible is floor(n/2). Because min(S, n-S) ≤ floor(n/2). So the maximum possible min_f (directed flow) would be T + floor(n/2). Let's compute: T = n(n-1)/2. So maximum possible min directed flow = n(n-1)/2 + floor(n/2). For n odd: floor(n/2) = (n-1)/2, so T + (n-1)/2 = n(n-1)/2 + (n-1)/2 = (n-1)(n+1)/2 = (n^2 -1)/2 = floor(n^2/2). For n even: floor(n/2) = n/2, so min = n(n-1)/2 + n/2 = n(n-1 + 1)/2 = n^2/2. So indeed matches the upper bound from n^2/2. So theoretically, if we can achieve for every pair (i,j) exactly S_ij = n/2 for n even, or S_ij = (n±1)/2 for n odd (i.e., as balanced as possible), then the minimum directed flow would be T + floor(n/2) = floor(n^2/2). And that would be optimal.
Thus the problem reduces to constructing an assignment (i.e., permutations for each block) such that for all distinct i,j, the number of columns where i > j is either floor(n/2) or ceil(n/2). For n even, we want exactly n/2 each. For n odd, we want either (n-1)/2 or (n+1)/2, but we want the minimum across pairs to be as large as possible, i.e., at least floor(n/2). So we need for each pair, the count where i > j should be at least floor(n/2). Since S_ij + S_ji = n, this is equivalent to S_ij ∈ {floor(n/2), ceil(n/2)}. So we need a tournament-like structure? Actually, we have n groups (vertices) and n columns, each column defines a total ordering (a permutation) of the vertices. For each ordered pair (i,j), we count how many columns where i is above j. This is like constructing n linear orders such that for any two distinct i,j, the number of orders where i precedes j is as balanced as possible.
This is reminiscent of designing "round-robin tournament" schedules or "balanced tournament designs". More precisely, we need an n x n matrix with entries being numbers from sets (which we later map to actual numbers), such that each column is a permutation, and for any two rows i,j, the number of columns where row i entry > row j entry is either floor(n/2) or ceil(n/2). That's achievable? Possibly by using the concept of "Latin squares" or "orthogonal arrays"? Let's explore.
We have n groups (rows). We need n columns. For each column, we assign a ranking to rows. The pairwise counts can be thought of as a matrix M where for each unordered pair, we have a win-loss record across n "games" (columns). We want each pair to have as balanced a record as possible.
If we view each column as a "match" where groups are ordered, then the number of columns where i beats j is S_ij. If we can have S_ij = n/2 for all i≠j when n even, that would imply S_ij = S_ji = n/2, which is symmetric. But that is impossible if n is odd because sum is odd, so one must be (n+1)/2 and the other (n-1)/2. For odd n, we could aim for each unordered pair to have counts ( (n+1)/2, (n-1)/2 ). However, is that possible for all pairs simultaneously? Consider three items and n=3 columns. Can we have for each of the three pairs (1,2), (1,3), (2,3) that the number of columns where 1 > 2 is 2 (i.e., 2/1)? But sum over columns for each pair is 3, so if we want min >=1? Actually for n=3, floor(n/2)=1. So we need each pair to have S_ij either 1 or 2, and min=1. That's automatically true because S_ij cannot be 0 or 3? But we must check if we can achieve S_ij ≥1 for all pairs. In example n=3, with three columns? Actually we have n=3 columns? Wait, n is number of groups and also number of blocks? n=3, we have 3 blocks. In the example, is it true that for any two groups, the number of blocks where group i has larger number than group j is either 1 or 2? Let's compute from example: groups: group1 (2,5,8), group2 (3,4,9), group3 (1,6,7). Columns: block1 (low) values: group1=2, group2=3, group3=1. In block1: order: group2(3) > group1(2) > group3(1). So comparisons: group2 > group1, group2 > group3, group1 > group3. block2: values: group1=5, group2=4, group3=6. Order: group3(6) > group1(5) > group2(4). So comparisons: group3 > group1, group3 > group2, group1 > group2. block3: values: group1=8, group2=9, group3=7. Order: group2(9) > group1(8) > group3(7). So comparisons: group2 > group1, group2 > group3, group1 > group3.
Now compute S_12 = number of blocks where group1 > group2: block1: 2>3? false; block2: 5>4 true; block3: 8>9? false. So S_12=1. S_21 = 2. S_13: block1: 2>1 true; block2:5>6 false; block3:8>7 true => S_13=2. S_31=1. S_23: block1:3>1 true; block2:4>6 false; block3:9>7 true => S_23=2. S_32=1. So indeed for each pair, one direction is 2, the other 1. So min=1 = floor(3/2)=1. Good.
So for n=3, we achieved the optimum with min=1? But earlier we computed min directed flow = T + min(S, n-S). T = n(n-1)/2 = 3*2/2 =3. So min f = 3 + 1 =4. That matches the example: min=4.
Now, can we always achieve S_ij ∈ {floor(n/2), ceil(n/2)} for all pairs? This is equivalent to constructing a set of n permutations of [n] such that for any two distinct i, j, the number of permutations where i precedes j is either ⌊n/2⌋ or ⌈n/2⌉. For even n, that would be exactly n/2 each. For odd n, it would be either (n-1)/2 or (n+1)/2. This is known as a "balanced tournament design" or "round-robin tournament schedule". Actually, consider a round-robin tournament with n teams, each pair plays exactly once. That doesn't match because we have n "rounds" (columns) and each round is a total ordering of all teams. In a round-robin schedule, each round typically consists of n/2 disjoint matches (if n even) or one team rests (if n odd). But here each round gives a linear order, which is like a ranking. The condition we need: for each pair (i,j), the number of rounds where i is ranked above j is as equal as possible. That is reminiscent of constructing a "tournament matrix" with each pair having equal wins/losses if possible. For even n, we want each pair to have exactly n/2 wins each. For odd n, each pair should have (n+1)/2 and (n-1)/2. Can we achieve this with n rounds? There is known concept of "round-robin tournament" where each team plays each other exactly once, but that's not the same. Here each round provides a complete ordering, so each pair gets a comparison in each round. So after n rounds, each pair has n comparisons. We want them as balanced as possible. This is like constructing a "balanced sequence" of permutations. Is it always possible? I recall something about "Latin squares" and "orthogonal Latin squares" giving balancedness. Actually, if we take the rows of a Latin square as permutations? Let's think.
We need n permutations π_1, π_2, ..., π_n on n elements such that for any two distinct elements a,b, the number of permutations where a precedes b is exactly n/2 (if n even) or as close as possible. This is known as a set of permutations with the "pairwise balanced" property. For even n, it is possible if and only if n is even? There's a known construction: take the permutations corresponding to cyclic shifts of a fixed order. For example, for n even, consider the identity permutation (1,2,...,n). Then for k=0,...,n-1, apply a cyclic shift: (1+k,2+k,...,n,1,...,k) modulo n? But then for any two distinct numbers i and j, the number of shifts where i precedes j? If we take all n cyclic shifts of the order (1,2,...,n), then for any i<j, in the identity i precedes j. After shifting, the relative order of i and j can change. More precisely, consider the set of all n cyclic permutations of the list (1,...,n). In each shift, the order is a rotation. For two distinct numbers i and j, how many rotations put i before j? Since the rotation just rotates the cyclic order, and the underlying cycle is (1,2,...,n) circular. In a circular arrangement, there are exactly n/2 arcs from i to j in the clockwise direction? Actually, if we consider the natural order modulo n, the relative order of i and j in a rotation depends on where the rotation starts. It is known that if you take all n rotations of a cyclic order of n items, then each unordered pair appears in both orders equally often when n is even? Let's compute for n=4: order (1,2,3,4). Rotations:
0: 1,2,3,4
1: 2,3,4,1
2: 3,4,1,2
3: 4,1,2,3
Now consider pair (1,2). In rotations: 0: 1 before 2; 1: 2 before 1? Actually rotation1: 2,3,4,1 -> order: 2,3,4,1 so 2 before 1; 2: 3,4,1,2 -> 1 before 2? The sequence is 3,4,1,2: so 1 appears before 2; 3: 4,1,2,3: 1 before 2. So counts: 1 before 2 in rotations 0,2,3 = 3 times; 2 before 1 in rotation1 = 1 time. So not balanced. So cyclic shifts don't give balance.
What about using all n! permutations? Obviously we can select n permutations that satisfy pairwise balance? But we are limited to exactly n permutations (columns). There's a known combinatorial design called "balanced tournament design" of order n, which schedules rounds such that each team plays each other exactly once, but that's different.
Maybe we can achieve the desired S_ij values by taking the rows of a Latin square? Let's formulate: We have n groups (rows) and n columns. Each column is a permutation. We want the matrix of "wins" between rows to be as balanced as possible.
Consider constructing an n x n matrix where the entry in row i, column k is a number from 1..n (like rank within column). Actually, we can assign each column a ranking from 1 (highest) to n (lowest). Then for rows i and j, we compare their ranks in column k: i > j if rank_i < rank_j? Since larger number indicates higher lab number? Actually careful: In a block (column), the numbers are increasing within the block from low to high? Wait, block k contains numbers from (k-1)n+1 to k n. So higher numbers within the block are larger. So if a group gets a larger number in that block, it means it is higher (since numbers represent height). In our earlier S_ij = number of columns where i's number > j's number. So i > j if M[i][k] > M[j][k]. So we can think of each column assigning to each row a distinct value from the block's set. To abstract, we can ignore the actual numbers and just think of a ranking: In column k, we assign each row a distinct integer "rank" from 1 to n, where rank r means the actual number is (k-1)n + r (since the numbers are consecutive). Actually, if we label the numbers within a block as 1..n in increasing order (1 smallest, n largest), then the actual number is (k-1)n + label. So M[i][k] > M[j][k] iff label_i > label_j. So we can represent each column as a permutation σ_k of [n] (the labeling of groups to ranks). Then for rows i, j, S_ij = number of columns k where σ_k(i) > σ_k(j). That is exactly the number of permutations where i is ranked higher than j.
Thus we need to choose n permutations σ_1,...,σ_n of [n] such that for all i≠j, the count of k with σ_k(i) > σ_k(j) is either floor(n/2) or ceil(n/2). Moreover, we also need the actual numbers to be the specific numbers from blocks; but that is automatically satisfied if we assign according to these permutations: for column k, assign the number (k-1)n + σ_k(i) to group i (or maybe reversed? careful: if we take label 1 as smallest, then actual number = (k-1)n + label. So if we want group i to have a higher number than group j in that column, we need label_i > label_j. So yes, we can define σ_k(i) as the label (1..n). Then the condition for S_ij is count of k with σ_k(i) > σ_k(j). So we need to find such permutations.
Now, is it always possible to have such a set? For n even, we want exactly n/2 each. For n odd, we want either (n-1)/2 or (n+1)/2, but we want the minimum across pairs to be floor(n/2). So it's enough that for each pair, the count is at least floor(n/2) (since sum = n, that implies it's at most ceil(n/2), but the min being floor(n/2) is equivalent to count >= floor(n/2) and count <= ceil(n/2)). So we need to avoid 0 or n extremes.
We can think of it as constructing a tournament matrix W where w_ij = number of columns where i > j. W is an integer matrix with w_ij + w_ji = n. We want w_ij ∈ {⌊n/2⌋, ⌈n/2⌉}. For even n, w_ij = n/2 for all i≠j. That means the matrix is a "regular tournament" where each team has exactly n/2 wins? Actually in a tournament (wins sum to n across each pair), if each pair splits equally, then each team's total wins (sum over j of w_ij) would be (n-1)n/2. Since there are n-1 opponents, each with n/2 wins, total wins per team = (n-1)n/2. But total wins across all teams is n(n-1)/2 * n? Wait, careful: each ordered pair i≠j contributes one win to w_ij. So total wins sum_{i≠j} w_ij = sum_{i≠j} n/2 = n(n-1)/2 * n? Actually number of ordered pairs i≠j is n(n-1). If each w_ij = n/2, then total wins = n(n-1)(n/2) = n^2(n-1)/2. But also total wins can be counted per column: each column (permutation) defines a total order, and the number of wins (i>j) in that column is the number of pairs where i appears above j. In a total order, for each unordered pair, exactly one direction is a win. So each column contributes C(n,2) wins. With n columns, total wins = n * C(n,2) = n * n(n-1)/2 = n^2(n-1)/2. So that matches. So such a perfectly balanced win matrix is possible if we can assign permutations such that each ordered pair has exactly n/2 wins. This is like constructing an "orthogonal array" or a "combinatorial design". For even n, a known construction uses the "round-robin tournament schedule" but each round is a perfect matching, not a total order. However, we can convert a round-robin schedule with n-1 rounds (for n odd) into something? Let's explore.
Alternatively, consider the set of all n! permutations. There exist subsets of permutations that are "pairwise balanced". This is related to the concept of "balanced permutations" or "balanced sequences". For even n, one can take the rows of a "Latin square" that is "mutually orthogonal" to its transpose? Maybe we can use a specific construction: let σ_k(i) = (i * k) mod (n+1) ??? Not sure.
Let's think about group theory: The set of all permutations of n elements form the symmetric group S_n. We want a subset of size n such that for any i≠j, the number of permutations where i precedes j is n/2 (or as balanced). This is reminiscent of "balanced permutations" or "alternating group"? Actually, consider the n cyclic permutations generated by adding a constant modulo n, but that gave imbalance for n=4. Maybe we need to use both the identity and its reverse? For example, for any n, if we take half the permutations as identity and half as reverse, then for a pair (i,j) with i<j, in identity i before j; in reverse j before i. If we have n permutations, with t copies of identity and n-t copies of reverse, then S_ij = t for i<j, and S_ij = n-t for i>j. So for i<j, S_ij = t, S_ji = n-t. To have min >= floor(n/2), we need t >= floor(n/2) and n-t >= floor(n/2) => t ∈ [floor(n/2), ceil(n/2)]. So t = floor(n/2) or ceil(n/2). Then each pair would have S_ij either t or n-t. For n even, t = n/2 gives exactly balanced. For n odd, t = (n-1)/2 or (n+1)/2 gives one direction (i<j) has (n-1)/2 and the other (n+1)/2. That would achieve min = floor(n/2). So if we simply take t copies of identity and n-t copies of reverse, we get such a property! But we need exactly n permutations. For n even, take n/2 copies of identity and n/2 copies of reverse. For n odd, take (n-1)/2 copies of identity, (n+1)/2 copies of reverse, or vice versa. That would give for any i<j, S_ij = number of identity copies = t, and S_ji = n-t. So for each pair, the min is min(t, n-t) = floor(n/2). Perfect! But we must check if it's allowed to have duplicate permutations? The problem does not forbid that two columns (blocks) have the same assignment pattern. In our construction, each block is a set of n distinct numbers, and we assign them to groups. If we use the same permutation multiple times, that means in multiple blocks we assign numbers to groups in the same order. That is allowed because each block has its own set of numbers, but we can assign the smallest number to the same group in multiple blocks, etc. There's no restriction that the assignment patterns across blocks must be distinct. So we can repeat permutations. That seems valid.
But wait: There is a subtlety: In each block, the numbers are specific: block k has numbers (k-1)n+1 to kn. If we assign the same permutation (e.g., identity) to multiple blocks, then in block k1 and k2, the group that gets the smallest number (label 1) will get the smallest numbers of those blocks, i.e., (k1-1)n+1 and (k2-1)n+1, which are distinct numbers. That's fine. So we can reuse the same permutation pattern.
Thus the construction: For each column (block) k = 1..n, we assign a permutation σ_k from a multiset consisting of floor(n/2) copies of identity permutation (i.e., σ(i)=i) and ceil(n/2) copies of reverse permutation (i.e., σ(i)=n+1-i). Actually careful: We need n columns. We'll designate some number t of columns to be identity, and n-t to be reverse. To achieve min = floor(n/2), we need t = floor(n/2) or ceil(n/2). Let's choose t = floor(n/2). Then n - t = ceil(n/2). So for each unordered pair (i,j) with i<j, in identity columns i > j? Wait, we need to define "label" meaning: label 1 is smallest number, label n is largest. In identity permutation, we assign group i the label i. That means group i gets the i-th smallest number in that block. So if i < j, then label i = i < j = label j, so group i gets a smaller number than group j. Since larger lab number corresponds to higher height, group i has lower height than group j. For water flow direction, we count when a lab from group A has higher number than lab from group B. So if group A has label larger, then it's higher. So for identity, for groups i and j with i<j, group i has smaller label -> lower height; group j has larger label -> higher height. Therefore, in identity column, group j > group i (since j's number > i's number). So S_j,i = 1 for that column if we consider identity? Actually S_{j,i} = number of columns where j > i. In identity column, since label_j > label_i, we have j > i. So S_{j,i} gets +1 for identity column. Similarly, in reverse permutation, we assign group i label n+1-i. Then for i<j, label_i = n+1-i > n+1-j = label_j? Since n+1-i > n+1-j when i<j. So group i gets larger label than group j, so group i > group j. So in reverse column, S_{i,j} gets +1.
Thus, in identity columns, the direction of "greater than" is: larger group index > smaller group index? Actually group index is just a label; we can rename groups arbitrarily. But we need to be consistent across columns. So if we use identity and reverse permutations, the resulting S_ij will depend on the ordering of group indices.
Specifically, let groups be numbered 1..n. For each identity column, for any i<j, we have j > i. So S_{j,i} increments. For each reverse column, for any i<j, we have i > j. So S_{i,j} increments.
Now suppose we have t identity columns and (n-t) reverse columns. Then for i<j:
S_{i,j} = number of reverse columns = (n-t) because in those columns i > j.
S_{j,i} = number of identity columns = t.
Thus min(S_{i,j}, S_{j,i}) = min(t, n-t) = floor(n/2) (if t = floor(n/2) or ceil(n/2) respectively). Good.
So the minimum directed flow will be T + floor(n/2). Since T = n(n-1)/2.
Thus we have a simple construction: For each block k (1-indexed from low to high), decide if it's an "identity" block or "reverse" block. Choose exactly t = floor(n/2) blocks to be identity, and the remaining n - t blocks to be reverse. Then assign numbers accordingly: In an identity block, assign the smallest number to group 1, second smallest to group 2, ..., largest to group n. In a reverse block, assign smallest to group n, second smallest to group n-1, ..., largest to group 1.
But careful: Our group indexing: We need to output n groups. We can arbitrarily label groups as 1..n in output. The construction yields groups based on these assignments. But we need to output groups (each as a set of n numbers). So we can directly compute: for group i (i from 1 to n), collect numbers from each block: if block k is identity, group i gets (k-1)*n + i (since i-th smallest); if block k is reverse, group i gets (k-1)*n + (n+1-i). So that gives each group exactly one number from each block, i.e., n numbers.
Now we must verify that this indeed gives the min directed flow = T + floor(n/2). Let's test with n=3: floor(3/2)=1, so t=1 identity, n-t=2 reverse. Choose 1 identity block, 2 reverse blocks. But we have 3 blocks. Which block is identity? The example had identity? Let's see: Example groups: group1:2,5,8; group2:3,4,9; group3:1,6,7. If we assign groups indices as 1,2,3 as they are in output? The output groups are listed: first group: 2 8 5, second: 9 3 4, third: 7 6 1. But let's see if it fits identity/reverse pattern. Sort blocks: block1 (1-3): group1 gets 2 (i=1?), group2 gets 3, group3 gets 1. That ordering: group1 got 2, group2 got 3, group3 got 1. That is not identity (identity would be group1:1, group2:2, group3:3). Nor reverse (reverse would be group1:3, group2:2, group3:1). So it's a different permutation. However, our identity/reverse construction might yield a different specific partition, but still should achieve the optimum. The example might be using a different set of permutations, but still balanced. So ours should be valid. Let's compute with our construction for n=3: t=1 identity, 2 reverse. Suppose we set block1 as identity, block2 and block3 as reverse. Then groups: group1 gets from block1:1, block2: reverse: (2-1)*3 + (3+1-1)=3+? Actually block2: numbers 4,5,6. Reverse: group1 gets smallest? Wait: For reverse, group i gets label (n+1-i). For n=3, reverse mapping: group1->3, group2->2, group3->1. So block2 reverse: group1 gets 3rd smallest => 6? Actually block2 numbers: 4 (label1),5(label2),6(label3). Group1 gets label3 => 6. Group2 gets label2 =>5. Group3 gets label1 =>4. Block3 reverse: numbers 7,8,9. Group1 gets label3 =>9, group2 label2=>8, group3 label1=>7. Block1 identity: group1 label1=>1, group2 label2=>2, group3 label3=>3. So groups: group1: {1,6,9}; group2: {2,5,8}; group3: {3,4,7}. That yields groups: group1: 1,6,9; group2: 2,5,8; group3:3,4,7. Now compute f between groups? Let's compute min directed flow? Use formula: for any two groups, S_ij counts columns where i > j. With t=1 identity, 2 reverse. For i<j: S_{i,j} = number of reverse columns =2; S_{j,i}=1. So min =1. Then min f = T + 1 = 3+1=4. So should be optimum. Check manually maybe? Compute f(group1, group2): group1 numbers {1,6,9}, group2 {2,5,8}. Count a>b: 6>2,6>5,9>2,9>5,9>8, also 1>? none. That's 5? Actually 6>2,6>5,9>2,9>5,9>8 =5. So f(1,2)=5. f(2,1)= total pairs 9 - 5 =4. min=4. f(1,3): group1 {1,6,9}, group3 {3,4,7}. Count a>b: 6>3,6>4,9>3,9>4,9>7 =>5; f(3,1)=4 => min=4. f(2,3): group2 {2,5,8}, group3 {3,4,7}: a>b: 5>3,5>4,8>3,8>4,8>7 =>5; f(3,2)=4 => min=4. So min is 4. So works.
Thus identity/reverse construction works.
But is it always optimal? We argued upper bound is floor(n^2/2). For n even, n^2/2 is integer; we need to achieve min = n^2/2. Let's test n=4 with this construction. n=4: floor(4/2)=2, so t=2 identity, 2 reverse. Then min S =2, T = n(n-1)/2 = 6, so min f = 6+2 =8, which is n^2/2 = 8. So we claim we can achieve min=8. Let's test a specific assignment: identity on two blocks, reverse on two. Choose block1 identity, block2 identity? Actually we need exactly 2 identity, 2 reverse. But which blocks? Order of blocks matters? The min f between groups should be independent of which blocks are identity/reverse as long as counts are correct, because S_ij only depends on number of reverse vs identity columns, not which block index. However, there might be a subtlety: The formula f(A,B) = T + S_AB where S_AB = number of columns where A > B (i.e., where A's label > B's label). That derivation assumed that for i>j (block index larger), always A_i > B_j. That holds regardless of assignment because numbers in higher block are larger than numbers in lower block. That part is fixed. So S_AB only from columns where we compare within the same block? Actually we derived f(A,B) = T + sum_{k} I(A_k > B_k). That is correct because contributions from cross-block pairs: for blocks i>j, always A_i > B_j, contributing exactly 1 for each such pair. The number of such cross-block pairs is exactly the number of ordered block pairs (i,j) with i>j, which is T = n(n-1)/2. And for i<j, always A_i < B_j, contributes 0. And for i=j, contributes I(A_k > B_k). So indeed f(A,B) = T + S_AB, where S_AB = number of blocks k with A's element > B's element. So S_AB depends only on the within-block comparisons. Therefore, the assignment of which specific block gets which permutation doesn't matter for S_AB; only the count of blocks where the permutation yields A > B. Since we have t identity columns (where for i<j, we have B > A? Actually careful: In identity columns, for groups indexed by numbers, we have: if group index i < j, then label_i < label_j, so group i < group j. So in identity column, the relation "A > B" is true exactly when group index of A > group index of B. In reverse column, it's the opposite: A > B when index A < index B. So for groups with indices i and j, S_ij = (number of reverse columns) if i < j, else (number of identity columns) if i > j. So indeed S_ij depends only on the counts of identity and reverse columns, not which blocks they are.
Thus the construction yields f(i,j) = T + (n - t) if i < j, and T + t if i > j? Actually check: For i<j: S_ij = number of reverse columns = n - t (since reverse gives i > j). So f(i,j) = T + (n - t). For i>j: S_ij = number of identity columns = t. So f(i,j) = T + t. Then the smaller of the two for the pair (i,j) is min(T + t, T + n - t) = T + min(t, n - t). So min overall = T + min(t, n-t). To maximize, we set t = floor(n/2) (or ceil). Then min = T + floor(n/2). So we achieve the bound.
Thus the construction is valid for any n.
But we must ensure that we output exactly n groups; we can choose group indices arbitrarily. The groups we produce will have numbers as described. That yields the optimum.
However, is there any hidden constraint that the groups must be unlabeled and the min over all ordered pairs of groups should be maximal? In our construction, the min directed flow is achieved for pairs where the direction is from the group with larger index to the smaller? Actually if t = floor(n/2) and n-t = ceil(n/2). Suppose n even, t = n/2, then min = T + n/2, and both directions have that value? Check: For i<j: f(i,j) = T + n - t = T + n/2; f(j,i) = T + t = T + n/2. So both equal, so min = T + n/2. For n odd, say n=3, t=1: then for i<j: f(i,j)=T+2, f(j,i)=T+1, so min = T+1. So the min is attained for the direction from larger index to smaller? Actually if i<j, f(i,j) = T + n - t, which for n=3, t=1 gives f(i,j)=T+2, f(j,i)=T+1, so min is the smaller one: f(j,i) = T+1. So the min occurs for pairs where direction is from larger index to smaller? Wait: i<j, then larger index is j, smaller is i. f(j,i) = T + t = T+1 (since j>i). So indeed min occurs for the direction from higher index to lower index when t < n/2? Actually if t < n/2, then t is floor, so t < n-t. Then for i<j, f(i,j) = T + (n-t) which is larger, f(j,i)= T + t which is smaller. So the min is f(j,i) (from larger index to smaller). Conversely, if we had chosen t = ceil(n/2) > n/2, then min would be from smaller index to larger. But we can choose either. So the min is uniform across all ordered pairs? Not exactly: For n odd, half the ordered pairs have flow = T+floor, half have flow = T+ceil. The min across all ordered pairs is the floor value. That's fine; we just need that minimum to be as large as possible. So we achieved that.
Thus the construction works and is simple.
But wait: The problem asks: "divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A ≠ B) is maximal." It does not say "ordered pairs" explicitly? It says "pairs of groups A and B (A ≠ B)". Usually "pairs" could be unordered, but they then say "the sum of units of water that can be sent from a group A to a group B is equal to ... Let's denote this value as f(A,B)". Then "min f(A,B) over all possible pairs of groups A and B (A ≠ B) is maximal." This could be interpreted as min over unordered pairs of the directed quantity f(A,B) where direction is from A to B? But they define f(A,B) as from A to B. So if they consider unordered pair {A,B}, they'd have two values f(A,B) and f(B,A). The phrase "min f(A,B) over all possible pairs of groups A and B (A ≠ B)" is ambiguous: it could mean consider all ordered pairs (A,B) with A≠B, and take the minimum of f(A,B). That's what we assumed. Alternatively, it could mean consider all unordered pairs {A,B}, and compute f(A,B) (with some order?), but that doesn't make sense because f(A,B) is directed. In the example, they list both f(X,Y) and f(Y,X), and then say "The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4." That suggests they consider the minimum among all these directed flows (both directions). So indeed it's min over ordered pairs.
Thus our construction yields optimum.
But is it always possible to achieve the upper bound floor(n^2/2) with any partition? Our construction gives exactly that bound. So the answer is yes. But we should double-check for n=2: n^2=4, floor(4/2)=2. Our construction: n=2, t = floor(2/2)=1 identity, n-t=1 reverse. So groups: identity block1? Actually we have two blocks: block1 (1,2) and block2 (3,4). Choose one identity, one reverse. Suppose block1 identity: group1 gets 1, group2 gets 2. Block2 reverse: group1 gets label2? For n=2, reverse mapping: group1->2, group2->1. So group1 gets 4, group2 gets 3. So groups: group1: {1,4}, group2: {2,3}. That yields f(1,2): group1 {1,4}, group2 {2,3}. Count a>b: 4>2,4>3 =>2; f(2,1)=4-2=2. min=2, optimal. So works.
Thus for any n, we can achieve the theoretical maximum min_f = floor(n^2/2). So the answer is to output groups constructed by this method.
But is there any case where this construction fails to achieve the bound? Let's verify the derivation of f(A,B) = T + S_AB holds for any assignment where each group has exactly one from each block. Yes, because cross-block contributions are fixed: for any two groups A and B, the number of pairs (a,b) with a∈A from a higher block than b∈B is exactly the number of ordered block pairs (i,j) with i>j, which is T, regardless of which specific numbers are taken from blocks. Indeed, for each i>j, the only pair is (a_i, b_j) where a_i is A's number from block i, b_j is B's number from block j. Since a_i > all numbers in block j (as block i > block j), and b_j is one of them, we always have a_i > b_j. So that contributes 1. For i<j, a_i < all numbers in block j, so a_i < b_j always, contributing 0. For i=j, depends on whether a_i > b_i. So total is T + (number of blocks where A's number > B's number). So the formula holds.
Thus, to maximize the minimum of f(A,B) over ordered pairs, we need to maximize the minimum over ordered pairs of T + S_AB, i.e., maximize min_{i≠j} S_ij. Since T is constant, we need to maximize min_{i≠j} S_ij. And S_ij + S_ji = n. So min(S_ij, S_ji) ≤ floor(n/2). So the maximum possible min S_ij is floor(n/2). So we need to achieve S_ij ≥ floor(n/2) for all ordered pairs? Actually min_{i≠j} S_ij is the smallest among all S_ij (ordered). Since S_ij for different ordered pairs may vary, we want the smallest of them as high as possible. That smallest cannot exceed floor(n/2) because for any unordered pair, one of S_ij or S_ji is at most floor(n/2). So the overall min across ordered pairs is at most floor(n/2). To achieve that bound, we need that for every unordered pair, both S_ij and S_ji are at least floor(n/2), i.e., each is either floor or ceil. That's exactly the condition we described. And the identity/reverse assignment achieves that with S_ij = t or n-t. So min = min(t, n-t) = floor(n/2). Good.
But is there any other requirement? For n odd, we need that for each unordered pair, the two S values are not both floor? Actually one is floor, one is ceil. That's fine. The min across all ordered pairs will be floor(n/2) if for every unordered pair, the smaller is exactly floor(n/2). That is achieved if for every unordered pair, the count of identity columns (or reverse) yields the smaller direction equal to floor. In our assignment, for i<j, S_ij = n-t, S_ji = t. If t = floor(n/2), then for i<j, S_ji = floor(n/2) (since t = floor), and S_ij = ceil(n/2). So the smaller is floor. For i>j, it's symmetric. So indeed for every ordered pair, either S = floor or ceil, and the min across all ordered pairs is floor. So achieved.
Thus this construction is valid.
But careful: In the identity/reverse assignment, for groups i and j, S_ij depends only on the relative order of indices. That means for each unordered pair, the smaller S is exactly floor(n/2) (provided t = floor(n/2)). So the min across ordered pairs is exactly floor(n/2). So the minimum directed flow is T + floor(n/2) = floor(n^2/2). So that is optimal.
Now we must consider if there is any possibility to get min > floor(n^2/2)? The upper bound is floor(n^2/2) because for any two groups, f(A,B) + f(B,A) = n^2, so min(f(A,B), f(B,A)) ≤ n^2/2. Since min is integer, ≤ floor(n^2/2). So yes.
Thus the construction yields optimum.
Therefore the solution: For given n, output n lines, each line containing n integers: for group i (1-indexed), the numbers are: for each block k from 1 to n, if (k is among the first t blocks? Actually we need to decide which blocks are identity and which are reverse. The assignment doesn't depend on which blocks; any selection of exactly t identity blocks and n-t reverse blocks works. To simplify, we can decide that for k from 1 to n: if k <= t, treat as identity? But we need exactly t identity blocks. But careful: We defined t = floor(n/2). Then we can set the first t blocks as identity, the rest as reverse. Or we can set the first n-t blocks as reverse, etc. But need to be consistent with t. Let's set identity blocks count = floor(n/2) (i.e., t = n//2). Then we can, for example, set for k = 1..n: if k <= t, then block k is identity; else reverse. Or we could alternate? It doesn't matter. But we need to ensure that the assignment yields S_ij = n-t for i<j? Wait, S_ij = number of reverse columns. In our assignment, reverse columns are those with k > t (if we set first t identity). So the count of reverse columns = n - t. That's consistent: S_ij for i<j = number of reverse columns = n-t. So that works.
But we need to compute actual numbers: For block k (1-indexed), its numbers are from (k-1)n + 1 to kn. In identity: group i gets (k-1)*n + i. In reverse: group i gets (k-1)*n + (n+1-i).
Thus groups: for i from 1 to n:
for k from 1 to n:
if k <= t: number = (k-1)*n + i
else: number = (k-1)*n + (n+1-i)
Collect these numbers into group i.
We need to output n lines, each with n numbers. Order within line can be any, but we can output in increasing order or as generated. Example output had numbers in arbitrary order, but we can output sorted or as we generate.
Let's test n=3: t = floor(3/2)=1. So block1 identity, blocks2,3 reverse. Then groups:
i=1: block1:1, block2 reverse: (2-1)*3 + (3+1-1)=3+? Actually compute: block2: (2-1)*3=3, numbers 4,5,6. reverse: group1 gets label3 => 3+3=6? Wait formula: (k-1)*n + (n+1-i) = (2-1)*3 + (4-1) = 3 + 3 =6. block3 reverse: (3-1)3=6, plus (4-1)=3? Actually n+1-i = 4-1=3, so number = 6+3=9. So group1: 1,6,9.
i=2: block1 identity:1? Actually (1-1)*3+2=2; block2 reverse: (2-1)*3 + (4-2)=3+2=5; block3 reverse: (3-1)*3 + (4-2)=6+2=8 -> group2: 2,5,8.
i=3: block1 identity: (1-1)*3+3=3; block2 reverse: (2-1)*3+(4-3)=3+1=4; block3 reverse: 6+1=7 -> group3:3,4,7.
So groups as we computed earlier: {1,6,9}, {2,5,8}, {3,4,7}. But the example output was different: {2,8,5}, {9,3,4}, {7,6,1}. Both are valid optimum partitions (min=4). So any such grouping works.
Thus the solution is straightforward: output groups using the described pattern.
But we need to be careful: The problem might require that the groups are unlabeled, i.e., we output any partition. The pattern yields a partition.
Now let's test n=4: t = 2 identity, 2 reverse. Suppose first 2 blocks identity, blocks3,4 reverse. Then groups:
i=1: block1: 1; block2: n=4 -> block2 identity: (2-1)*4+1=4+1=5? Actually (k-1)*n + i: block2: (2-1)*4=4, + i=1 =>5. block3 reverse: (3-1)*4=8, + (n+1-i)=5-? n+1-i =5-1=4? Actually n+1-i = 5-1=4, so number=8+4=12. block4 reverse: (4-1)4=12, +4=16? Actually 12+4=16. So group1: 1,5,12,16.
i=2: block1:2; block2:4+2=6; block3 reverse:8 + (5-2)=8+3=11; block4 reverse:12+3=15 => {2,6,11,15}
i=3: block1:3; block2:4+3=7; block3 reverse:8 + (5-3)=8+2=10; block4 reverse:12+2=14 => {3,7,10,14}
i=4: block1:4; block2:4+4=8; block3 reverse:8 + (5-4)=8+1=9; block4 reverse:12+1=13 => {4,8,9,13}
Now compute f between groups? Check some pair: group1 vs group2: group1 numbers {1,5,12,16}, group2 {2,6,11,15}. f(1,2): count a>b: 5>2, 12>2,12>6,12>11,16>2,16>6,16>11,16>15? also 1>? none, 5>? 5>2,5>? 5 not >6, etc. Let's systematically compute using formula: T = n(n-1)/2 = 43/2=6. S_12 for i=1,j=2 with 1<2? Since 1<2, S_12 = number of reverse columns = n-t = 2 (since t=2). So f(1,2)=T + 2 =8. f(2,1)=T + t =6+2=8. So both 8. So min=8. Good.
Thus this partition yields min f =8 = n^2/2 = 8. So optimum.
But is it possible that for some other assignment, the min could be larger than floor(n^2/2)? No, due to bound. So it's optimal.
Thus the solution is simply to output the groups constructed as above.
But wait, is there any nuance: The formula f(A,B) = T + S_AB assumed that within each block, the numbers are assigned arbitrarily but with exactly one per group. That's correct. So we reduced to balancing S_AB. The identity/reverse pattern gives S_AB either t or n-t. To achieve the optimum, we need t = floor(n/2) or ceil(n/2). So we can choose t = floor(n/2). That works for both even and odd.
But could there be a scenario where using a different multiset of permutations (not just identity and reverse) yields a higher min S_AB? The maximum min S_AB cannot exceed floor(n/2). So as long as we achieve floor(n/2), it's optimal. So identity/reverse is sufficient.
Thus implementation: read n, compute t = n // 2 (floor division). Then for i in 1..n (group index), we construct list of numbers. For k in 1..n:
if k <= t: number = (k-1)*n + i
else: number = (k-1)*n + (n - i + 1) // since n+1-i
We output each group's numbers (order within line can be as we generate, or maybe sorted ascending? The problem doesn't require any particular order. We'll output them in the order of blocks (k from 1 to n) or maybe sorted? It's okay. However, note that the example output order seems random; but we can output in any order. To be safe, we can output them in increasing order or in the order we generate (which is by increasing block index). Both are fine.
But check: For n=4 using above, group1 numbers: 1,5,12,16. That's not sorted; but it's okay. The problem statement: "In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want." So any order.
Thus we can implement straightforwardly.
But we must consider if n is odd, t = floor(n/2). Example n=3 gave t=1. That yields min=4, optimum. Good.
Now, is it always possible that using exactly t identity and n-t reverse yields S_ij such that the min is t? For odd n, t = (n-1)/2, n-t = (n+1)/2. Then min = t = (n-1)/2. So min S = (n-1)/2. Then min f = T + (n-1)/2 = n(n-1)/2 + (n-1)/2 = (n-1)(n+1)/2 = (n^2-1)/2 = floor(n^2/2). Good.
Thus solution is correct.
But we should also verify for n=1? Not allowed (n≥2). So fine.
Now, is there any hidden requirement that the groups should be unlabeled? Our construction labels groups 1..n, but we can output them in any order. The groups are sets, so we can output them as lines. It doesn't matter if we reorder the lines, as long as the partition is correct. So we can output groups in order i=1..n.
Now, test n=2: t = 1. Groups: block1 identity: (1-1)*2 + i => i; block2 reverse: (2-1)*2 + (2+1-i)=2 + (3-i) = 5-i? Actually compute: block2 numbers 3,4. Reverse: group1 gets label2 =>4, group2 gets label1=>3. So groups: {1,4} and {2,3}. That's optimal.
Now test n=5: t = 2. Then we have 2 identity blocks, 3 reverse blocks. Groups: i from 1 to 5. Compute numbers: For block k<=2 identity: number = (k-1)*5 + i. For k>2 reverse: number = (k-1)5 + (6-i). Then let's compute f between groups using formula: T = 54/2 =10. For i<j: S_ij = number of reverse blocks = 3 => f(i,j)=13; S_ji = number of identity blocks =2 => f(j,i)=12. So min =12. floor(n^2/2)= floor(25/2)=12. So optimum.
Check if any pair might have smaller due to some cross-block weirdness? Our derivation says no. So works.
Thus the solution is simply output groups as described.
But is there any alternative that yields a more balanced S_ij? Not needed.
Now, we must ensure that the output format: numbers separated by spaces. Should we print them sorted? It's not necessary. However, sometimes judges check for any valid partition, but they compare as sets. So any order is fine.
Thus algorithm complexity O(n^2) time and O(1) memory besides output.
Edge Cases: n=300 -> n^2=90,000, output is 90,000 numbers, which is fine.
Double-check: Are we sure that the bound floor(n^2/2) is indeed achievable for all n? Our construction yields it. So answer: yes.
But wait: The derivation that f(A,B) = T + S_AB used the fact that within each block, numbers are contiguous and groups have exactly one per block. That's true in our construction. However, we must also ensure that the groups we construct indeed have size n and cover all labs. Since we assign each number exactly once: For each block k, the assignment of labels to groups is a bijection (identity or reverse both are permutations). So each number in the block goes to exactly one group. So overall each lab is assigned to exactly one group. So valid partition.
Thus we are done.
But is there any other nuance: The problem statement: "The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B)." That's exactly what we used.
Now, we need to output the groups. In the example output, the groups are printed as "2 8 5", "9 3 4", "7 6 1". In our construction for n=3, we got groups {1,6,9}, {2,5,8}, {3,4,7}. That is different but also valid. So the judge should accept any optimal partition.
Now, maybe we should think: Could there be a scenario where the identity/reverse assignment leads to some f(A,B) less than the claimed min due to some cross-block pairs not being exactly 1? No, we already accounted.
Thus, final solution:
Read n.
t = n // 2 (integer division floor)
For group i in range(1, n+1):
numbers = []
for k in range(1, n+1):
base = (k-1)*n
if k <= t:
num = base + i
else:
num = base + (n - i + 1) # because n+1-i
numbers.append(num)
print(' '.join(map(str, numbers)))
But wait: For n odd, t = n//2, e.g., n=3, t=1. Then block1 identity, blocks2..n reverse. That's what we did. However, is there any reason to possibly swap identity/reverse? Both give same min. But we must ensure that we correctly compute the number of identity columns as t. In our loop condition k <= t, identity count = t. Then reverse count = n - t. That yields S_ij for i<j = n - t. Since t = floor(n/2), n - t = ceil(n/2). Then for i<j, f(i,j) = T + n - t, which is larger; f(j,i) = T + t, which is smaller. So the min is T + t. That equals T + floor(n/2). So fine.
Alternatively, if we set identity count = ceil(n/2) and reverse count = floor(n/2), then for i<j, f(i,j)=T+ floor, f(j,i)=T+ceil, min = T+floor. So same. It doesn't matter.
So our implementation with t = n//2 yields identity count = floor. So min = T + floor(n/2). Good.
Now, verify that for n even, say n=4, t=2, identity count=2, reverse=2, then for i<j, f(i,j)=T+2=8, f(j,i)=T+2=8, min=8. Good.
So correct.
Now, let's test n=2 with t=1: identity count=1, reverse=1. Then for i<j (1<2): f(1,2)=T + reverse count? Wait T=1? Actually T = n(n-1)/2 = 1. For i<j, S_12 = reverse count = 1? Since reverse gives i>j? Actually i<j, reverse gives i > j? We need to recalc: For i=1,j=2, in identity column: i gets label1, j label2 => i<j, so i not > j. In reverse column: i gets label2, j label1 => i > j. So S_12 = number of reverse columns =1. So f(1,2)= T+1 = 2. For f(2,1): S_21 = identity columns =1, so f= T+1 =2. So min=2. Good.
Now, we should double-check that for n odd, our assignment yields min f = T + t. For n=3, T=3, t=1 => min=4. Good.
Thus solution is done.
But wait, could there be a scenario where a different assignment yields min f larger than T + floor(n/2)? No, because min f across ordered pairs must be at most floor(n^2/2). Since T + floor(n/2) = floor(n^2/2). So that's max.
Thus we have a constructive proof of optimality.
Now, we just need to output. Since constraints up to 300, we can generate easily.
Potential issue: The example output's groups are different but still achieve min 4. In our output, min is also 4. So acceptable.
But maybe the judge expects a specific format? Usually problems with "If there are multiple optimal divisions, you can find any." So any is fine.
One more check: In the identity/reverse assignment, the groups are not necessarily sorted; but we can also output them sorted within each line for readability. That's fine.
Now, consider n=300, n^2=90,000. Print lines, each with 300 numbers. That's within typical output limits.
Now, confirm formula: number = (k-1)*n + i for identity; for reverse: (k-1)*n + (n - i + 1). Yes.
But note: When i runs from 1 to n, n - i + 1 runs from n to 1.
Now, we might want to alternate identity and reverse to perhaps make pattern more "balanced"? But not needed.
Thus final solution.
But before finalizing, let's think about whether there's any hidden catch: The derivation of f(A,B) = T + S_AB depends on the fact that for i>j (block indices), we always have A_i > B_j. That is true because all numbers in block i are larger than all numbers in block j when i>j. Since block i numbers range from (i-1)n+1 to i n, and block j numbers from (j-1)n+1 to j n, and i>j implies (j-1)n+1 to j n are all less than (i-1)n+1? Actually check: The maximum of block j is j n. The minimum of block i is (i-1)n+1. For i>j, we need to ensure j n < (i-1)n+1. Since i-1 >= j for i>j? If i = j+1, then (i-1)n = j n, so (i-1)n+1 = j n+1 > j n. So indeed, the smallest number in block i is greater than the largest number in block j when i > j. Because block intervals are contiguous and non-overlapping and increasing. So yes, all numbers in higher block are larger than all numbers in lower block. So cross-block contributions are fixed as described.
Thus the formula holds.
Hence the solution is correct.
Now, we should provide reasoning in answer: The maximum possible min f is floor(n^2/2). Achieve by taking groups such that each group contains exactly one lab from each "block" of n consecutive numbers, and arranging within each block either in increasing order or decreasing order, with exactly floor(n/2) blocks in increasing order and the rest in decreasing order.
Implementation details: We'll compute t = n // 2. Then for group i (1-indexed), for block k from 1 to n: if k <= t: number = (k-1)*n + i; else: number = (k-1)*n + (n - i + 1). Print these numbers.
But careful: When k <= t, that means we treat first t blocks as identity. But is there any reason to prefer that? It's arbitrary. However, note: The blocks are ordered by height: block1 lowest numbers, block n highest numbers. Our assignment gives identity to lower blocks and reverse to higher blocks. Could this affect f values? In our derivation, only counts matter, not which specific blocks. So it's fine.
Now, test n=3: t=1 => block1 identity, blocks2-3 reverse. Group1: from block1:1; block2: (1)*3+ (3+1-1)=? we'll compute: block2: base= (2-1)*3=3; reverse: n - i +1 = 3-1+1=3? Actually careful: n - i + 1: for i=1, that is 3; so number=3+3=6. Block3: base= (3-1)*3=6; n - i +1 =3; number=9. So group1 = {1,6,9}. Group2: block1:2; block2 reverse: base=3, n - i +1 for i=2: 3-2+1=2 =>5; block3: base=6, n - i +1 =2 =>8 => {2,5,8}. Group3: block1:3; block2 reverse: base=3, n - i +1 for i=3:1 =>4; block3: base=6, n - i +1=1 =>7 => {3,4,7}. This is valid.
Now, we could also choose blocks differently, but it's fine.
Now, we should output the groups. We can output each group's numbers in increasing order for clarity. That would be: group1: 1,6,9 sorted: 1,6,9; group2: 2,5,8; group3:3,4,7. That's fine.
But the example output had group1: 2 8 5, etc. So order not important.
Now, we should consider if the problem might require the groups to be output in some specific order? Usually no, just any partition.
Thus final answer: implement as described.
One more nuance: In the example, they listed groups that are not the identity/reverse pattern; but they might have used a different assignment (maybe using a Latin square). But our pattern is simpler.
Thus we can proceed.
Now, we must ensure that the output format: each line with n numbers separated by spaces. Possibly trailing space is okay. We'll print join.
Now, test for n=3: Our output (if we print groups in order i=1..n with numbers in the order of k) would be:
1 6 9
2 5 8
3 4 7
or sorted within each line: 1 6 9 etc. Both are fine.
Now, let's check if min f for this partition is indeed 4: we already did. So it's optimal.
Now, is there any possibility that for odd n, the optimum min f might be (n^2-1)/2 + 1? No, because that would exceed floor(n^2/2). So it's maximum.
Thus we are done.
But wait: Could there be a scenario where we cannot achieve exactly floor(n^2/2) because of parity constraints? Our construction shows we can. So it's always achievable.
Now, consider n=1? Not allowed. So fine.
Now, implementation details:
Read integer n.
t = n // 2
Initialize a list of lists groups = [ [] for _ in range(n) ]
For k in range(1, n+1):
base = (k-1) * n
if k <= t:
for i in range(1, n+1):
groups[i-1].append(base + i)
else:
for i in range(1, n+1):
groups[i-1].append(base + (n - i + 1))
Then print each group's numbers separated by space.
Complexities: O(n^2) time, O(n^2) memory for output.
Alternatively, we could generate row-wise directly: for i in range(n): for k in range(n): compute and store.
But careful: The order of numbers in each group as we append will be according to increasing k. That's fine.
Now, we should verify that groups have exactly n numbers and all numbers from 1 to n^2 appear exactly once. Because for each k, we assign each i exactly one number, and across k, we assign distinct numbers because base differs. So coverage.
Now, test for n=2: t=1. k=1 (<=1): identity: base=0; groups[0].append(0+1=1); groups[1].append(0+2=2) -> group1:1, group2:2. k=2 (else): reverse: base=2; groups[0].append(2 + (2-0? Actually for i=0 index? Let's index correctly: i from 0 to n-1, group index g. We'll use 0-index internally: For group index g (0..n-1) corresponding to i = g+1. Then identity: num = base + (g+1). Reverse: num = base + (n - (g+1) + 1) = base + (n - g). For n=2, reverse: num = base + (2 - g). For g=0: num=2+2=4; g=1: num=2+1=3. So groups: group0: [1,4]; group1: [2,3]. Good.
Now, test for n=5: t=2. k=1,2 identity; k=3,4,5 reverse. Should produce groups. Let's compute quick small check: For i=1 group: block1:1, block2: (2-1)*5+1 =5+1=6, block3 reverse: base= (3-1)*5=10, num=10 + (5 -1 +1?) Actually using 1-index: num = base + (n - i +1) =10 + (5-1+1)=10+5=15? Wait n - i +1 =5-1+1=5, so 15; block4: base=15, +5=20; block5: base=20, +5=25? That would be 25, but max number is 25. So group1: 1,6,15,20,25? But block3 gave 15, block4 20, block5 25. That seems plausible. But need to ensure that block4 base is (4-1)*5=15, plus 5 =20; block5 base=20, plus5=25. So group1: {1,6,15,20,25}. Group2: block1:2; block2:5+2=7; block3 reverse: base=10, n - i +1 = 5-2+1=4 =>14; block4: base=15, +4=19; block5: base=20, +4=24 => {2,7,14,19,24}. Group3: block1:3; block2:5+3=8; block3 reverse: 10+ (5-3+1=3)=13; block4:15+3=18; block5:20+3=23 => {3,8,13,18,23}. Group4: block1:4; block2:5+4=9; block3 reverse: 10+ (5-4+1=2)=12; block4:15+2=17; block5:20+2=22 => {4,9,12,17,22}. Group5: block1:5; block2:5+5=10; block3 reverse: 10+ (5-5+1=1)=11; block4:15+1=16; block5:20+1=21 => {5,10,11,16,21}. All numbers 1..25 appear exactly once. Nice pattern: groups have one from each block but reversed for higher blocks.
Now compute f between group1 and group2: T =10. For i=1<2, S_12 = reverse count = 3 (since blocks 3,4,5 reverse) -> f(1,2)=13; S_21= identity count =2 -> f(2,1)=12. So min=12. floor(25/2)=12. Good.
Thus construction works.
Therefore final solution code:
code
def solve():
import sys
n = int(sys.stdin.readline().strip())
t = n // 2 # number of identity blocks
groups = [[] for _ in range(n)]
for k in range(1, n+1):
base = (k-1) * n
if k <= t:
for i in range(1, n+1):
groups[i-1].append(base + i)
else:
for i in range(1, n+1):
groups[i-1].append(base + (n - i + 1))
out_lines = [' '.join(map(str, g)) for g in groups]
sys.stdout.write('\n'.join(out_lines))
That's it.
But careful: For n even, t = n//2. Then first t blocks are identity, remaining n-t = n/2 blocks are reverse. That gives equal counts. For n odd, t = floor(n/2), identity count = floor, reverse = ceil. So identity count <= reverse. In the formula, min f = T + identity count = T + floor(n/2). So okay.
Alternatively, we could set identity count = ceil(n/2) and reverse = floor. That would also work; we just need to ensure that min S = floor(n/2). If we set identity count = ceil, then for i<j, S_ij = reverse count = floor, so f(i,j)=T+floor, and f(j,i)=T+ceil, min = T+floor. So both ways yield same min. Our current assignment yields identity = floor (if t = n//2). So okay.
Now, consider if n=2: t=1 identity, reverse=1, min= T+1=2. Good.
Thus the answer.
But maybe we can simplify further: Just output groups where each group i has numbers: for k from 1 to n: if k <= n//2: print (k-1)*n + i else print (k-1)*n + (n - i + 1). That's it.
Now, confirm for n=3: n//2=1 => block1 identity, blocks2-3 reverse. Output groups as above.
Thus final solution.
Now, I will add explanation in the final answer.
One more check: Is it possible that for some n, the min f might be higher if we consider different assignment patterns that yield S_ij all equal to floor(n/2) even for odd n? For odd n, it's impossible because S_ij + S_ji = n is odd, so one must be floor, the other ceil. So min can't exceed floor. So optimal.
Thus answer complete.
We can implement.
Now, I will produce final answer with explanation. We need to partition the numbers (1,2,\ldots,n^2) into (n) groups of size (n) so that the minimum directed flow between any two different groups is as large as possible.
For any two groups (A,B), the flow (f(A,B)) counts the pairs ((a\in A,;b\in B)) with (a>b).
It is easy to see that (f(A,B)+f(B,A)=n^2). Hence the maximum possible minimum over all ordered pairs is at most (\left\lfloor n^2/2\right\rfloor).
Write the numbers in blocks of size (n):
block (k) contains ({(k-1)n+1,;(k-1)n+2,;\ldots,;k n}) for (k=1,\ldots,n) (block (1) is lowest).
If we make each group contain exactly one element from each block, then for groups (A,B) we have
[
f(A,B)=\frac{n(n-1)}{2}+S_{AB},
]
where (S_{AB}) is the number of blocks in which the element of (A) is larger than that of (B).
Here (\frac{n(n-1)}{2}) is the fixed contribution from pairs with elements from different blocks (the higher block always contributes a larger number).
Now (S_{AB}+S_{BA}=n). Therefore the minimum of (f(A,B)) over the two directions for the pair ({A,B}) is
[
\frac{n(n-1)}{2}+\min(S_{AB},,S_{BA}).
]
Thus we need to make (\min(S_{AB},S_{BA})) as large as possible for every unordered pair. The theoretical maximum is (\left\lfloor n/2\right\rfloor) because (S_{AB}+S_{BA}=n).
A simple construction achieving this bound is the following:
Take (t = \left\lfloor n/2\right\rfloor).
For the first (t) blocks use the identity assignment: in block (k\le t) give the (i)-th group the number ((k-1)n+i).
For the remaining (n-t) blocks use the reverse assignment: in block (k>t) give the (i)-th group the number ((k-1)n+(n-i+1)).
Then for any two groups (i<j) we have
(S_{ij}=n-t) (reverse blocks) and (S_{ji}=t) (identity blocks).
Hence (\min(S_{ij},S_{ji})=t=\left\lfloor n/2\right\rfloor). Consequently
The groups produced are a valid partition (each number appears exactly once) and the answer can be any optimal one, so this construction is acceptable.
Implementation
Read (n), compute (t=n//2). For each group (i=1\ldots n) collect numbers from each block (k=1\ldots n):
if (k\le t): number = ((k-1)n + i)
else: number = ((k-1)n + (n-i+1))
Print the (n) lines, each containing the (n) numbers of a group (order inside a line does not matter).
Complexity: (O(n^2)) time and output.
python
import sys
def solve() -> None:
n = int(sys.stdin.readline())
t = n // 2 # number of identity blocks
groups = [[] for _ in range(n)]
for k in range(1, n + 1):
base = (k - 1) * n
if k <= t:
# identity assignment
for i in range(1, n + 1):
groups[i - 1].append(base + i)
else:
# reverse assignment
for i in range(1, n + 1):
groups[i - 1].append(base + (n - i + 1))
out = '\n'.join(' '.join(map(str, g)) for g in groups)
sys.stdout.write(out)
if __name__ == '__main__':
solve()