codeforces 495D Sonya and Matrix
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的更多相关文章
- 【题解】Sonya and Matrix Beauty [Codeforces1080E]
[题解]Sonya and Matrix Beauty [Codeforces1080E] 传送门:\(Sonya\) \(and\) \(Matrix\) \(Beauty\) \([CF1080E ...
- Codeforces Round #495 (Div. 2) D. Sonya and Matrix
http://codeforces.com/contest/1004/problem/D 题意: 在n×m的方格中,选定一个点(x,y)作为中心点,该点的值为0,其余点的值为点到中心点的曼哈顿距离. ...
- Sonya and Matrix CodeForces - 1004D (数学,构造)
http://codeforces.com/contest/1004/problem/D 题意:网格图给定到中心点的曼哈顿距离数组, 求该图n,m及中心点位置 首先可以观察到距离最大值mx一定在某个角 ...
- Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)
https://codeforces.com/contest/1080/problem/E 题意 有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...
- Sonya and Matrix Beauty Codeforces - 1080E
https://codeforces.com/contest/1080/problem/E 比赛时候一个多小时码不出来... 来看遇到的困难: 1.没有能用的随机unsignedlonglong函数 ...
- Codeforces Round #495 (Div. 2) Sonya and Matrix
正常没有正方形的限制下,值为i的点个数4i 那么从0开始遍历,第一个不为4i的值就是min(x, y) 由于对称性我们姑且令x为这个值 我们先列举n*m=t的各种情况 对于一对n, m.我们已经知道n ...
- Sonya and Matrix Beauty CodeForces - 1080E (manacher)
大意: 给定$nm$字符串矩阵. 若一个子矩形每一行重排后可以满足每行每列都是回文, 那么它为好矩形. 求所有好矩形个数. 一个矩形合法等价于每一行出现次数为奇数的最多只有一个字符, 并且对称的两行对 ...
- Codeforces 714C. Sonya and Queries Tire树
C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...
- CodeForces 313C Ilya and Matrix
Ilya and Matrix Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
随机推荐
- hdu-3401-Trade-单调队列优化的DP
单调队列入门题... dp[i][j]:第i天.手中拥有j个股票时,获得的最大利润. 若第i天不买不卖:dp[i][j]=max(dp[i][j],dp[i-1][j]); 若第i天买 ...
- android选择图片或拍照图片上传到server(包含上传參数)
在9ria论坛看到的.还没測试,先Mark与大家分享一下. 近期要搞一个项目,须要上传相冊和拍照的图片.不负所望,最终完毕了! 只是须要说明一下,事实上网上非常多教程拍照的图片.都是缩略图不是非常清晰 ...
- UVALive 4223 / HDU 2962 spfa + 二分
Trucking Problem Description A certain local trucking company would like to transport some goods on ...
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
1.转自:https://blog.csdn.net/alinshen/article/details/78221567?utm_source=blogxgwz4 /* * 演示通过HttpClie ...
- SparkShuffle调优原理和最佳实践
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...
- CentOS_mysql8.0_错误
#参考资料 CSND:https://blog.csdn.net/y_server/article/details/78781177 博客园:http://www.cnblogs.com/testwa ...
- 在vue组件中style scoped中遇到的坑
在uve组件中我们我们经常需要给style添加scoped来使得当前样式只作用于当前组件的节点.添加scoped之后,实际上vue在背后做的工作是将当前组件的节点添加一个像data-v-1233这样唯 ...
- 安装lnmp前请先运行screen
当通过putty或者SecureCRT安装lnmp时, 网络突然掉线或者不小心putty被关掉等等原因, 造成lnmp安装过程被中断怎么办? 其实防止这种现象很简单, 只要在安装lnmp前执行scre ...
- org.apache.ibatis.binding.BindingException: Parameter ‘brOrderNo’ not found. Available parameters ar
最近使用 mybatis 写项目的时候遇到报错:org.apache.ibatis.binding.BindingException: Parameter 'brOrderNo' not found. ...
- Pimple相关的源码
已经有了非常好的Pimple的相关解析,建议先看下:Pimple - 一个简单的 PHP 依赖注入容器读 PHP - Pimple 源码笔记(上)读 PHP - Pimple 源码笔记(下) 这里通过 ...