Codeforces Round #495 (Div. 2) Sonya and Matrix
正常没有正方形的限制下,值为i的点个数4i
那么从0开始遍历,第一个不为4i的值就是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的更多相关文章
- 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix
题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...
- Codeforces Round #495 (Div. 2) D. Sonya and Matrix
http://codeforces.com/contest/1004/problem/D 题意: 在n×m的方格中,选定一个点(x,y)作为中心点,该点的值为0,其余点的值为点到中心点的曼哈顿距离. ...
- Codeforces Round #495 (Div. 2) C. Sonya and Robots
http://codeforces.com/contest/1004/problem/C 题意: 在一行上有n个数字,现在在最左边和最右边各放置一个机器人,左右机器人各有一个数字p和q.现在这两个机器 ...
- Codeforces Round #495 (Div. 2) B
题目链接:http://codeforces.com/contest/1004/problem/B B. Sonya and Exhibition time limit per test 1 seco ...
- Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】
任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...
- Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)
F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...
- Codeforces Round #540 (Div. 3)--1118C - Palindromic Matrix
https://codeforces.com/contest/1118/problem/C 在查找元素的时候,必须按4,2,1的顺序进行.因为,如果先找1,可能就把原来的4拆散了,然后再找4,就找不到 ...
- Codeforces Round #495 (Div. 2) A,B,C
A题 1.新添加一间酒店,要求酒店离已有的最近的一间酒店的距离恰好等于d 2.最左和最右必定存在合适的两种情况 3.酒店之间的情况就要判断两间酒店间的距离: 小于2d,表示无法在这两间酒店中间找到合适 ...
- Codeforces Round #697 (Div. 3) F. Unusual Matrix (思维,数学)
题意:给你一个矩阵\(a\)和\(b\),你可以对\(a\)的任意一行或任意一列的所有元素xor\(1\)任意次,问最终是否能够得到\(b\). 题解:由\(a\ xor\ b=c\),可得:\(a\ ...
随机推荐
- Redis 基本操作(一)
redis和普通的Key-Value结构不同,Redis的Key支持灵活的数据结构,除了strings,还有hashes.lists. sets 和sorted sets等结构.正是这些灵活的数据结构 ...
- 阿里云修改主机名(以centOS为例)
需要更改配置文件生效,修/etc/sysconfig/network里的 HOSTNAME=主机名(可自定义),重启生效. 如何修改? 1.[root@aliyunbaike ~]# cd /etc/ ...
- oracle database 9i/10g/11g 编程艺术 源代码下载
背景 在找这本书的源码,搜到提供的都是需要C币下载的.比较固执(其实是穷). 在这本书的前言中提到源代码可以在 www.appress.com 上下载. 下面是该书在该网站上的链接: https:// ...
- Const 关键字详解
const 标识符 在c++中作为常量修饰符 用来修饰 函数 变量 指针 const 修饰的变量不可以改变值 const 在修饰指针的时候 const 标识符出现在*的左边表示 指向的变量为常量不能 ...
- JS原生评分组件
JS原生评分组件 <html> <head> <meta http-equiv="Content-Type" content="text/h ...
- ASP.NET Core学习网站推荐
跟大家推荐一个不错的学习.NET Core 的网站,这个网站的视频是付费的,但是录视频的都是.NET Core的大佬们,个人觉得很不错推荐出来 video.jessetalk.cn
- Google+百度,自动识别知名人物的性别
最近有一个任务,需要采集一批知名学者的性别信息.该任务的难点在于提供学者信息的网站并不会主动标注学者的性别性别,因此只能靠别的方法了. 对一个普通人来说,在网上判断一个人的性别的最快的方式就是看他的照 ...
- 模拟MBR Grub故障修复
1. MBR故障修复 备份 mkdir /pp mount /dev/sdb1 /pp dd if=/dev/sda of=/pp/mrb.bak bs=512 count=1 破坏mrb dd ...
- Yii2 yiisoft/mongodb 手动安装
手动将yiisoft/mongodb下载到vendor/yiisoft目录(注意约束条件). 在vendor/yiisoft/extensions.php 中添加 'yiisoft/yii2-mong ...
- 关于一个flask的服务接口实战(flask-migrate,flask-script,SQLAlchemy)
前言 最近接到一个接收前端请求的需求,需要使用python编写,之前没有写过python,很多技术没有用过,在这里做一个学习记录,如有错误,请不了赐教. Flask Api文档管理 使用Falsk A ...