CF BUDDY
← Problems·

1236C · Labs

1300 · constructive algorithms, greedy, implementation

Problem: In order to do some research, n2n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 11 to n2n^2, such that the lab with the number 11 is at the lowest place, the lab with the number 22 is at the second-lowest place, \ldots, the lab with the number n2n^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 uu to the lab with the number vv if u>vu > v.

Now the labs need to be divided into nn groups, each group should contain exactly nn 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 AA to a group BB is equal to the number of pairs of labs (u,vu, v) such that the lab with the number uu is from the group AA, the lab with the number vv is from the group BB and u>vu > v. Let's denote this value as f(A,B)f(A,B) (i.e. f(A,B)f(A,B) is the sum of units of water that can be sent from a group AA to a group BB).

For example, if n=3n=3 and there are 33 groups XX, YY and ZZ: X={1,5,6},Y={2,4,9}X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z={3,7,8}Z = \{3, 7, 8\}. In this case, the values of ff are equal to:

  • f(X,Y)=4f(X,Y)=4 because of 525 \rightarrow 2, 545 \rightarrow 4, 626 \rightarrow 2, 646 \rightarrow 4,
  • f(X,Z)=2f(X,Z)=2 because of 535 \rightarrow 3, 636 \rightarrow 3,
  • f(Y,X)=5f(Y,X)=5 because of 212 \rightarrow 1, 414 \rightarrow 1, 919 \rightarrow 1, 959 \rightarrow 5, 969 \rightarrow 6,
  • f(Y,Z)=4f(Y,Z)=4 because of 434 \rightarrow 3, 939 \rightarrow 3, 979 \rightarrow 7, 989 \rightarrow 8,
  • f(Z,X)=7f(Z,X)=7 because of 313 \rightarrow 1, 717 \rightarrow 1, 757 \rightarrow 5, 767 \rightarrow 6, 818 \rightarrow 1, 858 \rightarrow 5, 868 \rightarrow 6,
  • f(Z,Y)=5f(Z,Y)=5 because of 323 \rightarrow 2, 727 \rightarrow 2, 747 \rightarrow 4, 828 \rightarrow 2, 848 \rightarrow 4.

Please, divide labs into nn groups with size nn, such that the value minf(A,B)\min f(A,B) over all possible pairs of groups AA and BB (ABA \neq B) is maximal.

In other words, divide labs into nn groups with size nn, such that minimum number of the sum of units of water that can be transported from a group AA to a group BB for every pair of different groups AA and BB (ABA \neq B) as big as possible.

Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values ff for some division.

If there are many optimal divisions, you can find any.

Input Format: The only line contains one number nn (2n3002 \leq n \leq 300).

Output Format: Output nn lines:

In the ii-th line print nn numbers, the numbers of labs of the ii-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.

Note: In the first test we can divide 99 labs into groups {2,8,5},{9,3,4},{7,6,1}\{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}.

From the first group to the second group we can transport 44 units of water (83,84,53,548 \rightarrow 3, 8 \rightarrow 4, 5 \rightarrow 3, 5 \rightarrow 4).

From the first group to the third group we can transport 55 units of water (21,87,86,81,512 \rightarrow 1, 8 \rightarrow 7, 8 \rightarrow 6, 8 \rightarrow 1, 5 \rightarrow 1).

From the second group to the first group we can transport 55 units of water (92,98,95,32,429 \rightarrow 2, 9 \rightarrow 8, 9 \rightarrow 5, 3 \rightarrow 2, 4 \rightarrow 2).

From the second group to the third group we can transport 55 units of water (97,96,91,31,419 \rightarrow 7, 9 \rightarrow 6, 9 \rightarrow 1, 3 \rightarrow 1, 4 \rightarrow 1).

From the third group to the first group we can transport 44 units of water (72,75,62,657 \rightarrow 2, 7 \rightarrow 5, 6 \rightarrow 2, 6 \rightarrow 5).

From the third group to the second group we can transport 44 units of water (73,74,63,647 \rightarrow 3, 7 \rightarrow 4, 6 \rightarrow 3, 6 \rightarrow 4).

The minimal number of the sum of units of water, that can be transported from one group to another is equal to 44. It can be proved, that it is impossible to make a better division.

Sample Cases

Case 1

Input

3

Output

2 8 5
9 3 4
7 6 1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback