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

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

She drew a n×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⋅m numbers). Note that Sonya will not give you n and m, so only the sequence of numbers in this matrix will be at your disposal.

Write a program that finds such ann×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 t(1≤t≤106) — the number of cells in the matrix.
The second line contains tintegers a1,a2,…,at (0≤ai<t) — the values in the cells in arbitrary order.
Output
In the first line, print two positive integers n and m (n×m=t) — the size of the matrix.
In the second line, print two integers xand y (1≤x≤n, 1≤y≤m) — the row number and the column number where the cell with 0
is located.

If there are multiple possible answers, print any of them. If there is no solution, print the single integer −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
Note

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

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

In the third example, a solution does not exist.

推数学公式

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef unsigned long long ull;
typedef long long ll;
//设0的坐标为(x,y)
//左上角的值为a=x+y-2;
//右下角的值为b=n+m-x-y;
//a+b=n+m-2
//b为最大值
//所以有y=n+m-b-x;
//n,m暴力求解x为最小的i使得a[i]!=i*4;
int t,n,m,a[],dp[],x,pos=-,val_x,val_y;
int main()
{
scanf("%d",&t);
for(int i=;i<t;i++)
{
scanf("%d",&x);
pos=max(pos,x);
a[x]++;
}
printf("\n");
int flag=;
for(int i=;;i++)
if(a[i]!=*i){val_x=i;break;}
for(int i=;i<=t;i++)
{
if(t%i) continue;
n=i,m=t/i;
val_y=n+m-pos-val_x;
memset(dp,,sizeof(dp));
for(int row=;row<=n;row++)
for(int col=;col<=m;col++)
dp[abs(row-val_x)+abs(col-val_y)]++;
for(int j=;j<=pos;j++)
if(a[j]!=dp[j]) goto eg;
flag=;
eg:;
if(flag) break;
}
if(flag) printf("%d %d\n%d %d\n",n,m,val_x,val_y);
else printf("-1\n");
return ;
}

codeforces 495D Sonya and Matrix的更多相关文章

  1. 【题解】Sonya and Matrix Beauty [Codeforces1080E]

    [题解]Sonya and Matrix Beauty [Codeforces1080E] 传送门:\(Sonya\) \(and\) \(Matrix\) \(Beauty\) \([CF1080E ...

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

    http://codeforces.com/contest/1004/problem/D 题意: 在n×m的方格中,选定一个点(x,y)作为中心点,该点的值为0,其余点的值为点到中心点的曼哈顿距离. ...

  3. Sonya and Matrix CodeForces - 1004D (数学,构造)

    http://codeforces.com/contest/1004/problem/D 题意:网格图给定到中心点的曼哈顿距离数组, 求该图n,m及中心点位置 首先可以观察到距离最大值mx一定在某个角 ...

  4. Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)

    https://codeforces.com/contest/1080/problem/E 题意 有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...

  5. Sonya and Matrix Beauty Codeforces - 1080E

    https://codeforces.com/contest/1080/problem/E 比赛时候一个多小时码不出来... 来看遇到的困难: 1.没有能用的随机unsignedlonglong函数 ...

  6. Codeforces Round #495 (Div. 2) Sonya and Matrix

    正常没有正方形的限制下,值为i的点个数4i 那么从0开始遍历,第一个不为4i的值就是min(x, y) 由于对称性我们姑且令x为这个值 我们先列举n*m=t的各种情况 对于一对n, m.我们已经知道n ...

  7. Sonya and Matrix Beauty CodeForces - 1080E (manacher)

    大意: 给定$nm$字符串矩阵. 若一个子矩形每一行重排后可以满足每行每列都是回文, 那么它为好矩形. 求所有好矩形个数. 一个矩形合法等价于每一行出现次数为奇数的最多只有一个字符, 并且对称的两行对 ...

  8. Codeforces 714C. Sonya and Queries Tire树

    C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...

  9. CodeForces 313C Ilya and Matrix

    Ilya and Matrix Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

随机推荐

  1. Python基本语法(基于3.x)

    Python的两种运行模式: 命令行模式,运行python,然后在命令行中输入python命令 程序脚本, 在命令行中输入 ./hello.py运行 Python是解释形语言,但可以通过工具打包成二进 ...

  2. (转)script标签到底该放在哪里

    一般script标签会被放在头部或尾部.头部就是<head>里面,尾部一般指<body>里[4-5]. 将script放在<head>里,浏览器解析HTML,发现s ...

  3. Angular4集成ng2-file-upload

      在Github上找到了一个支持Angular4好用的文件上传组件ng2-file-upload,这里简单介绍一下这个库的集成使用方案.  本文基于该组件的1.2.1版. 1. 安装 安装非常简单, ...

  4. 解码URLDecode和编码URLEnCode

    在前台往后台传递参数的时候,在前台进行编码,在后台接收参数的时候,用Decode进行解码: 如果url中包含特殊字符如:&.html标签 <tr><td>等导致url无 ...

  5. STM8S103之ADC

    如何快速了解ADC,查看Reference manual中ADC registers章节,初步了解到ADC ADC buffer register和ADC data register Analog W ...

  6. 使用rman恢复数据小结

    恢复前提有数据备份 以 alter database open resetlogs 开机以后多要做一次全备(以前的备份失效了) 恢复参数文件: restore spfile from '/home/o ...

  7. Incomplete types-不完全类型

    另外:前向声明中引入的类型为不完全类型(incomplete type),不完全类型只能以有限方式使用,只能用于定义指向该对象的指针和引用,只能用于声明使用该类型作为形参或返回类型的函数. Incom ...

  8. eclipse界面更改为黑色

    效果如下: 更改很简单,该两个配置就行了,如下图: 1.在window中打开Preferences,然后跟下图一样配置就行了.

  9. LaTex的几种数学符号字体以及相关说明

    \mathrm is the normal upright Roman font \mathnormal is the normal math italic font: $\mathnormal{a} ...

  10. select选中值,传this

    <select onChange = "a(this)"></select> function a(obj){ $(obj).find("opti ...