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.
这个题很考验数学思维量,自己真没想出来,转载一下了
链接:http://codeforces.com/contest/1004/problem/D
题意:给你t个数字(a1,a2......at),要你组成一个n x m的矩阵,这个矩阵满足这样的条件 ①矩阵里面的元素到“0”这个元素的曼哈顿距离为元素值大小。曼哈顿距离:两个点坐标差的绝对值之和。
现在问你的是输出n,m以及元素“0”的坐标(x,y);如果不存在这样的矩阵,输出“-1”。
注意:起点坐标为(1,1)。
分析:首先我们假设“0”的坐标为(x,y),它到(1,1)点的距离为a,到(n,m)点的距离为b;显而易见的是,b的大小就是元素ai的最大值。
因此我们可以等到这样的等式:
a=x-1+y-1;①
b=n-x+m-y;②
由①②可得 n+m=a+b+2;
所以 a=n+m-2-b;代入①得
y=n+m-b-x;
n,m显然很好球出来,b也已经知道,现在就差x如何求呢?
x稍微了解一下就能得出:让我们找到最小的i,是的列表中出现i的次数不等于4*i,则x=i;
大致的框架已经弄出来了,细节东西就要看自己了,我已经在代码中标明了一些细节东西,需要注意的。
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N],d[N];
int t;
int n,m;
int x,y;
int b;///矩阵角落到"0"的最大距离
///y=n+m-b-x
int main()
{
while(scanf("%d",&t)!=EOF){
memset(a,0,sizeof(a));
b=0;
for(int i=1;i<=t;i++){
int num;
scanf("%d",&num);
a[num]++;
b=max(b,num);
}
x=1;///x需要初始化为1,因为x的最小值为1,我们可能在下面的循环中,找不到这样的x
///从而导致x的值不确定.eg:1 0这一组数据。
for(int i=1;i<b;i++)
if(a[i]!=4*i){
x=i;
break;
}
int flag2=1;
for(n=1;n<=t;n++){
if(t%n==0){
m=t/n;
y=n+m-b-x;
memset(d,0,sizeof(d));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int temp=abs(i-x)+abs(j-y);
d[temp]++;
}
}
int flag=1;
for(int i=0;i<=b;i++){///这里i要从0开始比较,因为有可能出现多个"0"的情况.
if(a[i]!=d[i]){
flag=0;
break;
}
}
if(flag){
printf("%d %d\n",n,m);
printf("%d %d\n",x,y);
flag2=0;
break;
}
}
}
if(flag2)printf("-1\n");
}
return 0;
}