CF1004D.Sonya and Matrix

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.

Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so.

The Manhattan distance between two cells (x1x_1, y1y_1) and (x2x_2, y2y_2) is defined as ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|. For example, the Manhattan distance between the cells (5,2)(5, 2) and (7,1)(7, 1) equals to ∣5−7∣+∣2−1∣=3|5-7|+|2-1|=3.

Example of a rhombic matrix.

Note that rhombic matrices are uniquely defined by nn, mm, and the coordinates of the cell containing the zero.

She drew a n×mn\times m rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of n⋅mn\cdot m numbers). Note that Sonya will not give you nn and mm, so only the sequence of numbers in this matrix will be at your disposal.

Write a program that finds such an n×mn\times m rhombic matrix whose elements are the same as the elements in the sequence in some order.

由于索尼娅刚刚学习了矩阵的基础知识,她决定稍微玩一玩矩阵。

索尼娅构想出一种新型矩阵,她称之为“菱形矩阵”(rhombic matrices)。这类矩阵中恰好包含一个零,其余所有单元格中的数值均为该单元格到含零单元格的曼哈顿距离。具有相同数值的单元格构成一个菱形,因此索尼娅将此类矩阵命名为“菱形矩阵”。

两个单元格 (x1,y1)(x_1, y_1) 与 (x2,y2)(x_2, y_2) 之间的曼哈顿距离定义为 ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|。例如,单元格 (5,2)(5, 2) 与 (7,1)(7, 1) 之间的曼哈顿距离为 ∣5−7∣+∣2−1∣=3|5-7|+|2-1|=3。

菱形矩阵示例。

注意:菱形矩阵由 nn、mm 及含零单元格的坐标唯一确定。

她画出了一个 n×mn\times m 的菱形矩阵。她认为,若仅向你提供该矩阵中所有元素以某种任意顺序排列而成的序列(即长度为 n⋅mn\cdot m 的数字序列),你将无法复原出原始矩阵。注意,索尼娅不会告诉你 nn 和 mm,因此你仅能获得该矩阵中所有元素组成的序列。

请编写一个程序,找出某个 n×mn\times m 的菱形矩阵,使得其所有元素(不考虑顺序)与给定序列中的元素完全一致。

输入格式

The first line contains a single integer tt (1≤t≤1061\leq t\leq 10^6) — the number of cells in the matrix.

The second line contains tt integers a1,a2,…,ata_1, a_2, \ldots, a_t (0≤ai<t0\leq a_i \lt t) — the values in the cells in arbitrary order.

第一行包含一个整数 tt(1≤t≤1061\leq t\leq 10^6)—— 矩阵中的单元格数量。

第二行包含 tt 个整数 a1,a2,…,ata_1, a_2, \ldots, a_t(0≤ai<t0\leq a_i \lt t)—— 单元格中的数值,顺序任意。

输出格式

In the first line, print two positive integers nn and mm (n×m=tn \times m = t) — the size of the matrix.

In the second line, print two integers xx and yy (1≤x≤n1\leq x\leq n, 1≤y≤m1\leq y\leq m) — the row number and the column number where the cell with 00 is located.

If there are multiple possible answers, print any of them. If there is no solution, print the single integer −1-1.

第一行输出两个正整数 nn 和 mm(满足 n×m=tn \times m = t)——即矩阵的尺寸。

第二行输出两个整数 xx 和 yy(满足 1≤x≤n1\leq x\leq n,1≤y≤m1\leq y\leq m)——表示值为 00 的单元格所在的行号与列号。

若存在多个可行解,输出任意一个即可。若无解,则输出单个整数 −1-1。

输入输出样例

  • 输入#1

    20
    1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4

    输出#1

    4 5
    2 2
  • 输入#2

    18
    2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1

    输出#2

    3 6
    2 3
  • 输入#3

    6
    2 1 0 2 1 2

    输出#3

    -1

说明/提示

You can see the solution to the first example in the legend. You also can choose the cell (2,2)(2, 2) for the cell where 00 is located. You also can choose a 5×45\times 4 matrix with zero at (4,2)(4, 2).

In the second example, there is a 3×63\times 6 matrix, where the zero is located at (2,3)(2, 3) there.

In the third example, a solution does not exist.

你可以在图例中看到第一个示例的解。你也可以选择单元格 (2,2)(2, 2) 作为 00 所在的位置。你还可以选择一个 5×45\times 4 的矩阵,其中 00 位于 (4,2)(4, 2)。

在第二个示例中,存在一个 3×63\times 6 的矩阵,其中 00 位于 (2,3)(2, 3)。

在第三个示例中,不存在解。

输入解题思路,AI测评打分。不知道怎么写?

首页