「JSOI2015」symmetry
「JSOI2015」symmetry
我们先考虑构造出原正方形经过 \(4\) 种轴对称变换以及 \(2\) 种旋转变换之后的正方形都构造出来,然后对所得的 \(7\) 个正方形都跑一遍二维哈希,这样我们就可以通过哈希,在 \(O(n ^ 2)\) 时间内判断原正方形中是否存在某一类型的某一大小的子正方形。
但是如果我们枚举边长,复杂度就会达到 \(O(n ^ 3)\) 级别,显然过不了。
考虑优化:我们发现对于任意一种类型的正方形,它把最外面一圈去掉之后还是满足原来的性质,所以我们可以二分来求。需要注意的是我们不好同时计算奇数边长和偶数边长的正方形,所以要二分两次。
参考代码:
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
template < class T > inline T max(T a, T b) { return a > b ? a : b; }
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
typedef unsigned long long ull;
const int _ = 502;
const ull base1 = 19491001, base2 = 19260817;
int n; ull pw1[_], pw2[_], h[7][_][_];
inline ull gethash(int k, int x1, int y1, int x2, int y2) {
int w = x2 - x1 + 1;
return h[k][x2][y2] - h[k][x1 - 1][y2] * pw1[w] - h[k][x2][y1 - 1] * pw2[w] + h[k][x1 - 1][y1 - 1] * pw1[w] * pw2[w];
}
inline bool check_90(int x1, int y1, int x2, int y2) {
return gethash(0, x1, y1, x2, y2) == gethash(5, y1, n - x2 + 1, y2, n - x1 + 1);
}
inline bool check_180(int x1, int y1, int x2, int y2) {
return gethash(0, x1, y1, x2, y2) == gethash(6, n - x2 + 1, n - y2 + 1, n - x1 + 1, n - y1 + 1);
}
inline bool check_diag(int x1, int y1, int x2, int y2) {
return gethash(0, x1, y1, x2, y2) == gethash(1, x1, n - y2 + 1, x2, n - y1 + 1)
|| gethash(0, x1, y1, x2, y2) == gethash(2, n - x2 + 1, y1, n - x1 + 1, y2)
|| gethash(0, x1, y1, x2, y2) == gethash(3, y1, x1, y2, x2)
|| gethash(0, x1, y1, x2, y2) == gethash(4, n - y2 + 1, n - x2 + 1, n - y1 + 1, n - x1 + 1);
}
inline bool check_4(int x1, int y1, int x2, int y2) {
return check_180(x1, y1, x2, y2) && check_diag(x1, y1, x2, y2);
}
inline bool check_8(int x1, int y1, int x2, int y2) {
return check_90(x1, y1, x2, y2) && check_diag(x1, y1, x2, y2);
}
template < class T > inline bool check(T f, int x) {
for (rg int i = x; i <= n; ++i)
for (rg int j = x; j <= n; ++j)
if (f(i - x + 1, j - x + 1, i, j)) return 1;
return 0;
}
template < class T > inline int solve(T f) {
int res = 0, l, r, mid;
l = 0, r = (n - 1) >> 1;
while (l < r) {
mid = (l + r + 1) >> 1;
if (check(f, mid << 1 | 1)) l = mid; else r = mid - 1;
}
res = max(res, l << 1 | 1);
l = 1, r = n >> 1;
while (l < r) {
mid = (l + r + 1) >> 1;
if (check(f, mid << 1)) l = mid; else r = mid - 1;
}
res = max(res, l << 1);
return res;
}
int main() {
#ifndef ONLINE_JUDGE
file("cpp");
#endif
read(n);
for (rg int i = 1; i <= n; ++i)
for (rg int j = 1; j <= n; ++j) {
scanf("%1d", &h[0][i][j]);
h[1][i][n - j + 1] = h[0][i][j];
h[2][n - i + 1][j] = h[0][i][j];
h[3][j][i] = h[0][i][j];
h[4][n - j + 1][n - i + 1] = h[0][i][j];
h[5][j][n - i + 1] = h[0][i][j];
h[6][n - i + 1][n - j + 1] = h[0][i][j];
}
pw1[0] = 1; for (rg int i = 1; i <= n; ++i) pw1[i] = pw1[i - 1] * base1;
pw2[0] = 1; for (rg int i = 1; i <= n; ++i) pw2[i] = pw2[i - 1] * base2;
for (rg int k = 0; k < 7; ++k) {
for (rg int i = 1; i <= n; ++i) for (rg int j = 1; j <= n; ++j) h[k][i][j] += h[k][i - 1][j] * base1;
for (rg int i = 1; i <= n; ++i) for (rg int j = 1; j <= n; ++j) h[k][i][j] += h[k][i][j - 1] * base2;
}
printf("%d %d %d %d %d\n", solve(check_8), solve(check_90), solve(check_4), solve(check_180), solve(check_diag));
return 0;
}
「JSOI2015」symmetry的更多相关文章
- 「JSOI2015」串分割
「JSOI2015」串分割 传送门 首先我们会有一个贪心的想法:分得越均匀越好,因为长的绝对比短的大. 那么对于最均匀的情况,也就是 \(k | n\) 的情况,我们肯定是通过枚举第一次分割的位置,然 ...
- 「JSOI2015」isomorphism
「JSOI2015」isomorphism 传送门 我们还是考虑树哈希来判同构. 但是我们需要使用一些特殊的手段来特殊对待假节点. 由于是无向树,我们首先求出重心,然后以重心为根跑树哈希. 此处我们不 ...
- 「JSOI2015」地铁线路
「JSOI2015」地铁线路 传送门 第一问很简单:对于每条线路建一个点,然后所有该条线路覆盖的点向它连边,权值为 \(1\) ,然后它向所有线路上的点连边,权值为 \(0\) . 然后,跑一边最短路 ...
- 「JSOI2015」染色问题
「JSOI2015」染色问题 传送门 虽然不是第一反应,不过还是想到了要容斥. 题意转化:需要求满足 \(N + M + C\) 个条件的方案数. 然后我们就枚举三个数 \(i, j, k\) ,表示 ...
- 「JSOI2015」圈地
「JSOI2015」圈地 传送门 显然是最小割. 首先对于所有房子,权值 \(> 0\) 的连边 \(s \to i\) ,权值 \(< 0\) 的连边 \(i \to t\) ,然后对于 ...
- 「JSOI2015」最小表示
「JSOI2015」最小表示 传送门 很显然的一个结论:一条边 \(u \to v\) 能够被删去,当且仅当至少存在一条其它的路径从 \(u\) 通向 \(v\) . 所以我们就建出正反两张图,对每个 ...
- 「JSOI2015」套娃
「JSOI2015」套娃 传送门 考虑贪心. 首先我们假设所有的套娃都互相不套. 然后我们考虑合并两个套娃 \(i\),\(j\) 假设我们把 \(i\) 套到 \(j\) 里面去,那么就可以减少 \ ...
- 「JSOI2015」非诚勿扰
「JSOI2015」非诚勿扰 传送门 我们首先考虑一名女性选中她列表里第 \(x\) 名男性的概率(假设她列表里共有 \(s\) 名男性): \[ P = p \times (1 - p) ^ {x ...
- 「JSOI2015」salesman
「JSOI2015」salesman 传送门 显然我们为了使收益最大化就直接从子树中选大的就好了. 到达次数的限制就是限制了可以选的子树的数量,因为每次回溯上来都会减一次到达次数. 多种方案的判断就是 ...
随机推荐
- python笔记-02
Python基础知识 —————————————— A,B,先把A乘以3,然后加上B,最后在加上列表A A = [1, 2, 3, 4, 5, 6] 赋值 B = [1, 2, 3] 变量 定义一个变 ...
- 二分-G - 4 Values whose Sum is 0
G - 4 Values whose Sum is 0 The SUM problem can be formulated as follows: given four lists A, B, C, ...
- [codeigniter4]Upgrading from 3.x to 4.x
CodeIgniter 4 is a rewrite of the framework, and is not backwards compatible. It is more appropriate ...
- 用Object.prototype.toString.call(obj)检测对象类型原因分析
用Object.prototype.toString.call(obj)检测对象类型原因分析 更新时间:2018年10月11日 08:46:33 投稿:laozhang 我要评论 在本 ...
- centos7 tar.gz zip 解压命令
tar负责打包,gzip负责压缩 tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个, ...
- PCB常见的拓扑结构
1.点对点拓扑 point-to-point scheduling 该拓扑结构简单,整个网络的阻抗特性容易控制,时序关系也容易控制,常见于高速双向传输信号线:常在源端加串行匹配电阻来防止源端的二次反射 ...
- 用OpenCV读取摄像头
首先插入摄像头 在电脑中查看 #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp ...
- Asp.net的WebForm的落后技术
本文链接:https://bbs.csdn.net/topics/392077893 因为webform确实,企图通过在html标签中的runat="server",在iis接受请 ...
- 2.2 logistic回归损失函数(非常重要,深入理解)
上一节当中,为了能够训练logistic回归模型的参数w和b,需要定义一个成本函数 使用logistic回归训练的成本函数 为了让模型通过学习来调整参数,要给出一个含有m和训练样本的训练集 很自然的, ...
- ajax post
前端 $.post("url",JSON.stringify({ a:a, ... }), function(data){ }, 'json'); 后端 @PostMapping( ...