一 原题
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 (x1x1, y1y1) and (x2x2, y2y2) is defined as |x1−x2|+|y1−y2||x1−x2|+|y1−y2|. 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.
![](https://i-blog.csdnimg.cn/blog_migrate/59b19bd5e1b8464b7f549e0fdc81e980.png)
Note that rhombic matrices are uniquely defined by nn, mm, and the coordinates of the cell containing the zero.
She drew a n×mn×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⋅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×m rhombic matrix whose elements are the same as the elements in the sequence in some order.
The first line contains a single integer tt (1≤t≤1061≤t≤106) — the number of cells in the matrix.
The second line contains tt integers a1,a2,…,ata1,a2,…,at (0≤ai<t0≤ai<t) — the values in the cells in arbitrary order.
In the first line, print two positive integers nn and mm (n×m=tn×m=t) — the size of the matrix.
In the second line, print two integers xx and yy (1≤x≤n1≤x≤n, 1≤y≤m1≤y≤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.
20 1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4
4 5 2 2
18 2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1
3 6 2 3
6 2 1 0 2 1 2
-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×4 matrix with zero at (4,2)(4,2).
In the second example, there is a 3×63×6 matrix, where the zero is located at (2,3)(2,3) there.
In the third example, a solution does not exist.
二 分析
在a*b个格子组成的矩形中,选定一个格子(x,y),任意格子(i,j)标记为abs(i-x)+abs(j-y)。现在给你n个数,问你是否存在恰好由这n个数构成的矩形。
矩形由一组(r,c,x,y)决定:长,宽,0所在的横纵坐标。如果矩形是无限的,那么标记为i的格子数应该为4*i。通过这个结论,我们可以知道min(x,y)(假设0所在的格子在矩形的左上角)。接着枚举r,输入n个数中的最大值d=r+c-min(x,y),得到一组(r,c,x,y)后验证一下是否合法。
三 代码
/*
AUTHOR: maxkibble
LANG: c++
PROB: cf 1004D
*/
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
const int maxn = 1e6 + 5;
int n, cnt[maxn], d, mind, nx, ny, px, py, c2[maxn];
inline bool judge(int r, int c, int x, int y) {
memset(c2, 0, sizeof(c2));
if (x < 1 || x > r || y < 1 || y > c) return false;
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) c2[abs(i - x) + abs(j - y)]++;
}
for (int i = 0; i <= d; i++) {
if (cnt[i] != c2[i]) return false;
}
return true;
}
int main() {
// freopen("d.in", "r", stdin);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
cnt[x]++;
d = std::max(d, x);
}
if (cnt[0] != 1) {
puts("-1"); return 0;
}
// 0 is in mind-th row or mind-th column
mind = 1;
while (cnt[mind] == 4 * mind) mind++;
for (int i = 2 * mind - 1; i * i <= n; i++) {
if (n % i != 0) continue;
int r = i, c = n / i, x = mind, y = r - mind + c - d;
if (judge(r, c, x, y)) nx = r, ny = c, px = x, py = y;
if (judge(r, c, y, x)) nx = r, ny = c, px = y, py = x;
if (nx) break;
}
if (!nx) puts("-1");
else printf("%d %d\n%d %d\n", nx, ny, px, py);
return 0;
}