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

http://codeforces.com/contest/1004/problem/D

题意:

在n×m的方格中,选定一个点(x,y)作为中心点,该点的值为0,其余点的值为点到中心点的曼哈顿距离。

现在给出t个点的值,问是否可以确定一个由t个点组成的方格,方格中的值由这t个点组成,如果有,则任一输出一个方格的规模n、m和中心点的坐标x、y。

思路:

参考了https://blog.csdn.net/FSAHFGSADHSAKNDAS/article/details/80951796的题解。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 const int maxn = 1e6+5;
 7 
 8 int mx, t, n, m, x, y, c[maxn], tmp[maxn];
 9 
10 
11 int main()
12 {
13     //freopen("in.txt","r",stdin);
14     scanf("%d",&t);
15     for(int i=0;i<t;i++)
16     {
17         int a; scanf("%d",&a);
18         mx = max(mx, a);
19         c[a]++;
20     }
21     for(int i=1;i<=t;i++)
22     {
23         if(c[i]!=(i<<2))
24         {
25             x = i;
26             break;
27         }
28     }
29     for(int n=1;n<=t;n++)
30     {
31         m = t/n;
32         if(n*m!=t)  continue;
33         y = n+m-x-mx;
34         memset(tmp,0,sizeof(tmp));
35         for(int i=1;i<=n;i++)
36         for(int j=1;j<=m;j++)
37         {
38             int d = abs(x-i)+abs(y-j);
39             tmp[d]++;
40         }
41         bool flag = true;
42         for(int i=0;i<=n+m;i++)
43         {
44             if(tmp[i]!=c[i])  {flag=false;break;}
45         }
46         if(flag)
47         {
48             printf("%d %d
",n,m);
49             printf("%d %d
",x,y);
50             return 0;
51         }
52     }
53     puts("-1");
54     return 0;
55 }
原文地址:https://www.cnblogs.com/zyb993963526/p/9302085.html