正常没有正方形的限制下,值为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. firefox下载文件弹出框之终极解决方案-vbs模拟键盘操作

    由于近期一直被firefox的保存文件弹出框困扰,摸索尝试过几种方法,已有的方法可以跑通但是对对效果不太满意,因此一直在寻找合适的解决办法. 最近发现了也可以通过VBS来处理弹出框,速度也不错,其原理 ...

  2. 爬虫之Beautifulsoup及xpath

    1.BeautifulSoup (以 Python 风格的方式来对 HTML 或 XML 进行迭代,搜索和修改) 1.1 介绍 Beautiful Soup提供一些简单的.python式的函数用来处理 ...

  3. SpringBoot自动装配的原理

    1.SpringApplication.run(AppConfig.class,args);执行流程中有refreshContext(context);这句话. 2.refreshContext(co ...

  4. IF()判断变量是否为空

    一 变量是字符串,判断为空 第一种:strs == null string strs = "test"; if (strs == null) { //这里是为空的字符串,返回你指定 ...

  5. 二维码生成(QRCode.js)

    什么是 QRCode.js? QRCode.js 是一个用于生成二维码的 JavaScript 库.主要是通过获取 DOM 的标签,再通过 HTML5 Canvas 绘制而成,不依赖任何库. 基本用法 ...

  6. C++ Primer学习笔记_1_快速入门

    C++快速入门 一 编写简单程序 // main是操作系统唯一显示调用的函数int main() {/**return返回的值是一个状态指示器 0:成功 非0:返回错误给OS*以echo $?命令可以 ...

  7. 463. Island Perimeter (5月29日)

    解答 class Solution { public: int islandPerimeter(vector<vector<int>>& grid) { int num ...

  8. MySQL常用参数说明(持续更新)

      ##innodb correlate   innodb_flush_log_at_trx_commit value: 0,[1],2 effect: control the flush opera ...

  9. 误操作yum导致error: rpmdb解决方法

    错误:[root@test ~]# yum makecache error: rpmdb: BDB0113 Thread/process 18967/139716328294400 failed: B ...

  10. php的基础知识(四)

    14.数组: 索引数组: 下标就是数字开始的. $arr = ['a','b','c',1,2,3]; 关联数组: $arr = [ 'a' => 'b', 'c' => 'd'; 'e' ...