c.Labs Codeforces Round #593 (Div. 2)

48 篇文章 0 订阅

In order to do some research, n2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n2, 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 n2

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(Y,Z)=4
  • because of 4→3, 9→3, 9→7, 9→8
  • ,
  • f(Z,X)=7
  • because of 3→1, 7→1, 7→5, 7→6, 8→1, 8→5, 8→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 minf(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

    Copy

    (copy)
    3
    
    Output

    Copy

    (copy)
    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

    ).

    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.

    题意:为了做一些研究,n2实验室建在一座不同高度的山上。我们用1到n2的整数来列举它们,比如1是最低的,2是第二低的,n2是最高的。为了在实验室之间运输水,每对实验室之间都要建管道。如果u是>v,那么管道一次最多只能输送一个单位的水,从编号为u的实验室输送到编号为v的实验室。现在实验室需要分成n组,每个组应该包含n个实验室。不同组的实验室可以互相输送水。的和单位的水,可以发送从a组B组的数量等于对实验室(u, v)的实验室你从a组,实验室与v是B组和u >。让我们用f(A,B)来表示这个值(即f(A,B)是A组向B组输送的水的单位之和)。例如,如果n=3,有3组X, Y和Z: X={1,5,6},Y={2,4,9}, Z={3,7,8}。在这种情况下,f的值等于:f (X, Y) = 4,因为5 2、4、5 6 2 6 4 f (X, Z) = 2,因为5 3、6 3 f (Y, X) = 5,因为2 1 4 1 9 1 9 5 9 6 f (Y, Z) = 4,因为4 3 9 3 9 7,9 8 f (Z, X) = 7,因为3 1 1 7、7 5,6,7 8 1 8 5 8 6 f (Z, Y) = 5 3 2,因为7 2 7 4,8 2 8 4。请把实验室分成n个组,每个组的大小为n,这样A组和B组的最小值(A,B)都是最大的。换句话说,把实验室分成n个大小为n的组,这样每对不同的a组和B组(a B)可以从a组运送到B组的水的单位和的最小数量。注意,上面的示例并没有演示最优除法,但是它演示了如何计算某些除法的f值。如果有很多最优划分,你可以找到任何一个。唯一的一行包含一个数字n (2 n 300)。输出输出n行:在第i行中打印n个数字,第i组实验室的数字,按你想要的任何顺序。如果有多个答案,最大限度地减少从一个组到另一个组的水的单位和,你可以打印任何一个。

    • .
  • 思路:第一行前面一半依次取每行最小的,后面依次取每行最大的,然后接下来的行按第一行前面一半依次递增,后面一半递减,这样每行相距其他行差距最小

    #include <cstdio>
    #include<iostream>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include<set>
    using namespace std;
    #define ll long long
    const int maxn=2e4;
    #define mod 1000000007
    int a[maxn];
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n/2;i++)
        {
            a[i]=n*(i-1)+1;
        }
        for(int i=n/2+1;i<=n;i++)
        {
            a[i]=i*n;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=1;j<=n/2;j++)
            {
                //if(j==n) printf("%d\n",a[j]-i);
               printf("%d ",a[j]+i);
            }
            for(int j=n/2+1;j<=n;j++)
            {
                if(j==n) printf("%d\n",a[j]-i);
                else printf("%d ",a[j]-i);
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值