Codeforces Round #495 (Div. 2):D. Sonya and Matrix(暴力)

time limit per test  2 seconds
memory limit per test  256 megabytes
input  standard input
output  standard output

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 (x1x1y1y1) and (x2x2y2y2) is defined as |x1x2|+|y1y2||x1−x2|+|y1−y2|. For example, the Manhattan distance between the cells (5,2)(5,2) and (7,1)(7,1) equals to |57|+|21|=3|5−7|+|2−1|=3.

Example of a rhombic matrix.

Note that rhombic matrices are uniquely defined by nnmm, 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 nmn⋅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.

Input

The first line contains a single integer tt (1t1061≤t≤106) — the number of cells in the matrix.

The second line contains tt integers a1,a2,,ata1,a2,…,at (0ai<t0≤ai<t) — the values in the cells in arbitrary order.

Output

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 (1xn1≤x≤n1ym1≤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.

Examples
input
20
1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4
output
4 5
2 2
input
18
2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1
output
3 6
2 3
input
6
2 1 0 2 1 2
output
-1


题意:给你T个数,要你构造一个n*m的矩阵满足①其中有且只有一个0;②其它所有位置上的数字正好等于它到0的曼哈顿距离;③矩阵中所有数字出现次数刚好和给出的T个数一致,例如题目中的图,如果能构造出来,输出任意一种(只需要输出n和m还有0的位置),否则输出-1


先假设这个矩阵无限大,那么肯定有4个1、8个2、12个3……4*n个n

所以可以先判断从哪个数字开始缺,例如1,4,8,11,就是3的时候就开始不全了,这样就可以确定0的位置(p, q)一定满足min(p, q)=3,直接对这些特定的位置暴力check就ok了,只要找到合法的就输出,找不到就是-1

哦对了n和m的大小也是直接暴力


#include<stdio.h>
#include<stdlib.h>
int sum[1000005];
int main(void)
{
	int T, n, m, i, j, x, y, p, now, temp;
	scanf("%d", &T);
	now = 0;
	for(i=1;i<=T;i++)
	{
		scanf("%d", &x);
		sum[x]++;
		if(sum[x]==1)
			now++;
	}
	if(sum[0]!=1)
	{
		printf("-1\n");
		return 0;
	}
	for(i=1;i<=2005;i++)
	{
		if(sum[i]>4*i)
		{
			printf("-1\n");
			return 0;
		}
		if(sum[i]<4*i)
			break;
	}
	p = i;
	for(n=1;n*n<=T;n++)
	{
		m = T/n;
		if(T%n || n<p*2-1 || m<p*2-1)
			continue;
		x = y = p;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				temp = abs(i-x)+abs(j-y);
				sum[temp]--;
				if(sum[temp]==0)
					now--;
				if(sum[temp]==-1)
					now++;
			}
		}
		if(now==0)
		{
			printf("%d %d\n%d %d\n", n, m, x, y);
			return 0;
		}
		for(y++;y+p-1<=m;y++)
		{
			for(i=1;i<=n;i++)
			{
				temp = abs(i-x)+abs(1-y);
				sum[temp]--;
				if(sum[temp]==0)
					now--;
				if(sum[temp]==-1)
					now++;
				temp = abs(i-x)+abs(m+1-y);
				sum[temp]++;
				if(sum[temp]==0)
					now--;
				if(sum[temp]==1)
					now++;
			}
			if(now==0)
			{
				printf("%d %d\n%d %d\n", n, m, x, y);
				return 0;
			}
		}
		for(y--,x++;x+p-1<=n;x++)
		{
			for(i=1;i<=m;i++)
			{
				temp = abs(1-x)+abs(i-y);
				sum[temp]--;
				if(sum[temp]==0)
					now--;
				if(sum[temp]==-1)
					now++;
				temp = abs(n+1-x)+abs(i-y);
				sum[temp]++;
				if(sum[temp]==0)
					now--;
				if(sum[temp]==1)
					now++;
			}
			if(now==0)
			{
				printf("%d %d\n%d %d\n", n, m, x, y);
				return 0;
			}
		}
		x--;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				temp = abs(i-x)+abs(j-y);
				sum[temp]++;
				if(sum[temp]==0)
					now--;
				if(sum[temp]==1)
					now++;
			}
		}
	}
	printf("-1\n");
	return 0;
}
/*
10
1 0 1 2 3
2 1 2 3 4
*/


猜你喜欢

转载自blog.csdn.net/jaihk662/article/details/80939688

相关文章