码迷,mamicode.com
首页 > 其他好文 > 详细

D - Sonya and Matrix

时间:2020-05-28 12:59:01      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:font   class   tchar   amp   onclick   note   hide   called   msu   

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 |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 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 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≤n, 1ym1≤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

Note

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.

 

题意:构造一个n*m的数组,将给定的长为N的序列的每个全部填入到数组中,使得数组中的每个数字X到0位置的曼哈顿距离等于X

题解:

  直接构造一个数组不好构造出来,那么就理解为是否存在一个n*m的数组,使得数组存的所有数刚好等于输入的序列,而画图可以发现完整的图形中,数i的个数一定是4*i

  那么,我们就先记录每个数出现的次数和最大值max,然后枚举一下1~N,找到N的因子K,那么因子的乘积K* (N/K)就是数组的大小,而最大值一定是在边角上,

  那么,通过图形可以发现 0 点的坐标之一 x (或者y) 一定是第一个不满足4*I的数字,另一个就是y(或者x) = n+m-max-x,

  那么,0点坐标也就出来了,那我们再遍历一下数组,看当前n*m的数组中需要每个数的个数是否都与给定数字的个数的相吻合,如果吻合就输出

  反之,就继续遍历

 

技术图片
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include<cctype>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define all(vc) vc.begin() , vc.end()
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lc now<<1
#define rc now<<1|1
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
using namespace std;
const int mod = 998244353;
const int mxn = 1e6 +7;
const int inf = 1e9;
ll _,n,m,t,k,u,v,w,ans,cnt,ok,lim,len,tmp,last;
struct node {int u,v;}e[mxn];
int a[mxn] , vis[mxn] ;
void solve()
{
    if(vis[0]!=1){
        cout<<-1<<endl;return ;
    }
    int l , r , x = 1, y; ok = 1 ;
    for(int i=1;i<=n;i++){
        if(vis[i]>4*i){
            cout<<-1<<endl;return ;
        }
        if(vis[i]<4*i) {
            x = i ; break;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(n%i) continue ;
        m = n/i ; ///
        y = i-x + m - last ;
        memset(a,0,sizeof(a)) ;
        for(int j=1;j<=i;j++)
        {
            for(int k=1;k<=m;k++)
            {
                tmp = abs( j-x ) + abs( k-y );
                a[tmp]++;
            }
        }
        int flag = 1 ;
        for(int j=1;j<=last;j++){
            if(a[j]!=vis[j]){
                flag = 0 ; break;
            }
        }
        if(flag){
            cout<<i<<" "<<m<<endl<<x<<" "<<y<<endl;
            ok=0;break;
        }
    }
    if(ok) cout<<-1<<endl;
}
int main()
{
    while(cin>>n)
    {
        memset(vis,0,sizeof(vis));
        last = 0 ;
        for(int i=1;i<=n;i++){
            cin>>k , vis[k]++;
            last = max(last,k);
        }
        solve();
    }
}
View Code

 

D - Sonya and Matrix

标签:font   class   tchar   amp   onclick   note   hide   called   msu   

原文地址:https://www.cnblogs.com/Shallow-dream/p/12979904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!