正常没有正方形的限制下,值为i的点个数4i

那么从0开始遍历,第一个不为4
i的值就是min(x, y)

由于对称性我们姑且令x为这个值

我们先列举n*m=t的各种情况

对于一对n, m。我们已经知道n,m,x

再由于对称性,我们假设距离(x,y)最远的点在(n, m)。(当然也可能在(1,m))

现在知道了(n,m)到(x,y)为max(a[I])

列方程就能求出y了

然后再暴力验证就好了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
using namespace std;
const int N = 1e6 + 5;
const int INF = 0x3f3f3f3f;
typedef long long ll;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1 int A[N];
int mp[N];
int maxNum; struct Line{
int k, b;
Line(int a=0, int _b=0):k(a), b(_b){}
int Count(int x1, int x2, int y1, int y2) {
int L = k*x1 + b;
int R = k*x2 + b;
if(L > R) swap(L, R); x1 = L; x2 = R;
if(y1 < x1) {
if(y2 < x1) return 0;
else if(y2 <= x2)
return y2 - x1 + 1;
else return x2 - x1 + 1;
} else if(y1 <= x2) {
if(y2 <= x2)
return y2 - y1 + 1;
else return x2 - y1 + 1;
} else
return 0;
}
};
bool Test(int n, int m, int x) { if(n < 2*x-1 || m < 2*x-1)
return false;
int y = n + m - maxNum - x;
if(y < x || m - y < x - 1)
return false; // printf("%d %d\n", n, m);
for(int i = x; i <= maxNum; ++i) {
int all = 0;
int l1 = max(1, x - i);
int l2 = min(n, x + i);
int r1 = max(1, y - i);
int r2 = min(m, y + i); Line t1(-1, i+x+y);
all += t1.Count(l1, l2, r1, r2); Line t2(1, i-x+y);
all += t2.Count(l1, l2, r1, r2); Line t3(-1, -i+x+y);
all += t3.Count(l1, l2, r1, r2); Line t4(1, -i-x+y);
all += t4.Count(l1, l2, r1, r2); // printf("%d ", all);
if(n - x >= i) all --;
if(y > i) all --;
if(m - y >= i) all --;
// printf("%d %d\n", i, all);
if(all != mp[i])
return false;
}
std::printf("%d %d\n%d %d\n", n, m, x, y);
return true;
}
int main() {
int t;
while(~scanf("%d", &t)) {
memset(mp, 0, sizeof(mp));
maxNum = -1;
for(int i = 0; i < t; ++i) {
scanf("%d", &A[i]);
mp[A[i]] ++;
maxNum = max(maxNum, A[i]);
} bool flag = true; int X;
for(int i = 0; ; ++i) {
int id = i; int num = mp[i];
if(id == 0) {
flag &= num == 1;
} else {
flag &= num == 4*i;
} if(flag == false) {
X = i;
break;
}
}
// for(int i = 0; i < t; ++i) printf("%d ", mp[i]); printf("\n");
// printf("%d\n", X); if(X == 0) {
printf("-1\n");
continue;
}
flag = false;
for(int i = 1; i <= sqrt(t) && !flag; ++i) {
if(t % i == 0) {
int n = i; int m = t / i; flag = Test(n, m, X);
if(!flag) {
flag = Test(m, n, X);
}
}
}
if(!flag) {
printf("-1\n");
}
}
return 0;
}

Codeforces Round #495 (Div. 2) Sonya and Matrix的更多相关文章

  1. 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

    题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...

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

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

  3. Codeforces Round #495 (Div. 2) C. Sonya and Robots

    http://codeforces.com/contest/1004/problem/C 题意: 在一行上有n个数字,现在在最左边和最右边各放置一个机器人,左右机器人各有一个数字p和q.现在这两个机器 ...

  4. Codeforces Round #495 (Div. 2) B

    题目链接:http://codeforces.com/contest/1004/problem/B B. Sonya and Exhibition time limit per test 1 seco ...

  5. Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

    任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...

  6. Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

    F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...

  7. Codeforces Round #540 (Div. 3)--1118C - Palindromic Matrix

    https://codeforces.com/contest/1118/problem/C 在查找元素的时候,必须按4,2,1的顺序进行.因为,如果先找1,可能就把原来的4拆散了,然后再找4,就找不到 ...

  8. Codeforces Round #495 (Div. 2) A,B,C

    A题 1.新添加一间酒店,要求酒店离已有的最近的一间酒店的距离恰好等于d 2.最左和最右必定存在合适的两种情况 3.酒店之间的情况就要判断两间酒店间的距离: 小于2d,表示无法在这两间酒店中间找到合适 ...

  9. Codeforces Round #697 (Div. 3) F. Unusual Matrix (思维,数学)

    题意:给你一个矩阵\(a\)和\(b\),你可以对\(a\)的任意一行或任意一列的所有元素xor\(1\)任意次,问最终是否能够得到\(b\). 题解:由\(a\ xor\ b=c\),可得:\(a\ ...

随机推荐

  1. StringJoiner

    示例一 public class StringJoinerTest1 { public static void main(String[] args) { StringJoiner joiner = ...

  2. oracle空间分析

    相交 sdo_relate(t.geom, sdo_geometry(:geometry,null),\'mask=ANYINTERACT\')=\'TRUE\'

  3. wordpress上传含中文文件名出现乱码

    一.首先到FTP里面找到wp-admin/includes/file.php这个文件. 二.查找wp_handle_upload在文件里面找到以下代码. function wp_handle_uplo ...

  4. IE下页面左偏移并页头空出一行解决方法

    在其它浏览器下显示正常,包括360浏览器,在IE下,页面向左偏移,通过firebug查看,head标签为空,并且head标签里面的内容都跑到body标签内了,原因是有bom头,访问的页面或是加载,包含 ...

  5. MyBatis之properties配置

    这些属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素来传递.例如: <properties resource=" ...

  6. Unity 游戏框架搭建 (二十一) 使用对象池时的一些细节

    上篇文章使用SafeObjectPool实现了一个简单的Msg类.代码如下: class Msg : IPoolAble,IPoolType { #region IPoolAble 实现 public ...

  7. Vue.js与 ASP.NET Core 服务端渲染功能整合

    http://mgyongyosi.com/2016/Vuejs-server-side-rendering-with-aspnet-core/ 原作者:Mihály Gyöngyösi 译者:oop ...

  8. 上白泽慧音(tarjan,图的染色)

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...

  9. chromium之message_pump_default

    看看头文件,默认的消息泵,该类实现了MessagePump的四个接口 class MessagePumpDefault : public MessagePump { public: MessagePu ...

  10. (Nagios)-check_openmanage[Dell]

      Nagios->check_openmanage[Dell R7*] 2014年11月13日 下午 07:44 需求介绍: 透过Nagios监控Dell R7系列服务器硬件状态 环境信息: ...