正常没有正方形的限制下,值为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. ListView 中嵌套 GridView

    1.主布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...

  2. 【题解】洛谷P3435 [POI2006] OKR-Periods of Words(KMP)

    洛谷P3435:https://www.luogu.org/problemnew/show/P3435 思路 来自Kamijoulndex大佬的解释 先把题面转成人话: 对于给定串的每个前缀i,求最长 ...

  3. js取整、四舍五入等数学函数

    js只保留整数,向上取整,四舍五入,向下取整等函数1.丢弃小数部分,保留整数部分parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math. ...

  4. 阿里云修改主机名(以centOS为例)

    需要更改配置文件生效,修/etc/sysconfig/network里的 HOSTNAME=主机名(可自定义),重启生效. 如何修改? 1.[root@aliyunbaike ~]# cd /etc/ ...

  5. MongoTemplate复合条件查询

    分. 排序.按时间查询 Query query = new Query();        //必须条件        Criteria c = Criteria.where("VINID& ...

  6. 定位权限授权 - iOS

    关于介入地图相关功能后会遇到类似定位的子功能,由此引来了此定位权限授权相关. 首先,需要导入 CoreLocation 的框架并创建管理对象从而实现后续的相关操作; #import <CoreL ...

  7. openresty 配置 mongodb 可操作插件

    1.下载lua-resty-mongol https://github.com/bigplum/lua-resty-mongol 2.配置_mongo.conf文件,在conf创建_mongo.con ...

  8. 【读书笔记 - Effective Java】02. 遇到多个构造器参数时要考虑用构建器

    类有多个可选参数的解决方案: 1. 重叠构造器模式可行,但是当有许多参数的时候,客户端代码会很难编写,并且仍然较难以阅读. 2. JavaBeans模式,调用一个无参构造器来创造对象,然后调用sett ...

  9. Jquery无刷新上传单个文件

    function ajax_photo(photo_type){        $(document).on('change','#sitephoto',function(){             ...

  10. linux进程篇 (三) 进程间的通信3 IPC通信

    3 IPC通信 用户空间 进程A <----无法通信----> 进程B -----------------|--------------------------------------|- ...