[JSOI2008]Blue Mary的战役地图
当看到n <= 50 的时候就乐呵了,暴力就行了,不过最暴力的方法是O(n7)……然后加一个二分边长达到O(n6logn),然后我们接着优化,把暴力比对改成O(1)的比对hash值,能达到O(n5logn),到勉强能过……不过我们还可以在优化一下,把第一个矩阵中所有边长为 l 的子矩阵的hash值都存到一个数组中,然后sort一下,接着我们在枚举第二个矩阵的子矩阵,然后在数组中用lower_bound的查询就行。这样的话复杂度应该是O(n3log(n2) * logn)了。
~~求一个矩阵的哈希值就是每一行的哈希值之和~~
- #include<cstdio>
- #include<iostream>
- #include<cmath>
- #include<algorithm>
- #include<cstring>
- #include<cstdlib>
- #include<cctype>
- #include<vector>
- #include<stack>
- #include<queue>
- using namespace std;
- #define enter puts("")
- #define space putchar(' ')
- #define Mem(a) memset(a, 0, sizeof(a))
- typedef long long ll;
- typedef unsigned long long ull;
- typedef double db;
- const int INF = 0x3f3f3f3f;
- const int eps = 1e-;
- const int maxn = ;
- const ull base = ; //请无视
- inline ll read()
- {
- ll ans = ;
- char ch = getchar(), last = ' ';
- while(!isdigit(ch)) {last = ch; ch = getchar();}
- while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
- if(last == '-') ans = -ans;
- return ans;
- }
- inline void write(ll x)
- {
- if(x < ) x = -x, putchar('-');
- if(x >= ) write(x / );
- putchar(x % + '');
- }
- int n, a[][maxn][maxn];
- ull has[][maxn][maxn];
- ull f[maxn], b[maxn * maxn];
- int cnt = ;
- ull calc(int x, int y, int l, bool flag)
- {
- ull ret = ;
- for(int i = x; i <= x + l - ; ++i) ret += has[flag][i][y + l - ] - has[flag][i][y - ] * f[l];
- return ret;
- }
- bool judge(int x)
- {
- cnt = ;
- for(int i = ; i <= n - x + ; ++i)
- for(int j = ; j <= n - x + ; ++j)
- b[++cnt] = calc(i, j, x, );
- sort(b + , b + cnt + );
- for(int i = ; i <= n - x + ; ++i)
- for(int j = ; j <= n - x + ; ++j)
- {
- ull ha = calc(i, j, x, );
- if(*lower_bound(b + , b +cnt + , ha) == ha) return ;
- }
- return ;
- }
- int main()
- {
- n = read();
- for(int k = ; k <= ; k++)
- for(int i = ; i <= n; ++i)
- for(int j = ; j <= n; ++j) a[k][i][j] = read();
- f[] = ;
- for(int i = ; i <= n; ++i) f[i] = f[i - ] * base;
- for(int k = ; k <= ; ++k)
- for(int i = ; i <= n; ++i)
- for(int j = ; j <= n; ++j) has[k][i][j] = has[k][i][j - ] * base + a[k][i][j];
- int L = , R = n;
- while(L + < R)
- {
- int mid = (L + R) >> ;
- if(judge(mid)) L = mid;
- else R = mid - ;
- }
- write(judge(L + ) ? L + : L); enter;
- return ;
- }
[JSOI2008]Blue Mary的战役地图的更多相关文章
- bzoj1567: [JSOI2008]Blue Mary的战役地图
将矩阵hash.s[0]忘了弄成0,输出中间过程发现了. hash.sort.判重.大概这样子的步骤吧. #include<cstdio> #include<cstring> ...
- BZOJ 1567: [JSOI2008]Blue Mary的战役地图( 二分答案 + hash )
二分答案, 然后用哈希去判断... ------------------------------------------------------------------------- #include ...
- BZOJ 1567: [JSOI2008]Blue Mary的战役地图
1567: [JSOI2008]Blue Mary的战役地图 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1011 Solved: 578[Sub ...
- BZOJ 1567: [JSOI2008]Blue Mary的战役地图 矩阵二维hash
1567: [JSOI2008]Blue Mary的战役地图 Description Blue Mary最近迷上了玩Starcraft(星际争霸) 的RPG游戏.她正在设法寻找更多的战役地图以进一步提 ...
- [JSOI2008]Blue Mary的战役地图(二分+哈希)
Blue Mary最近迷上了玩Starcraft(星际争霸) 的RPG游戏.她正在设法寻找更多的战役地图以进一步提高自己的水平. 由于Blue Mary的技术已经达到了一定的高度,因此,对于用同一种打 ...
- B1567 [JSOI2008]Blue Mary的战役地图 二分答案+hash
一开始以为是dp,后来看了一下标签...二分答案?之前也想过,但是没往下想,然后之后的算法就顺理成章,先求出第一个地图的所有子矩阵的hash值,然后求第二个,在上一个地图例二分查找,然后就没了. 算法 ...
- BZOJ1567 [JSOI2008]Blue Mary的战役地图 二分答案 哈希
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1567 题意概括 给出两个n*n的数字矩阵,问最大公共正方形边长. 题解 先二分答案一个m,对于每一 ...
- [JSOI2008]Blue Mary的战役地图——全网唯一一篇dp题解
全网唯一一篇dp题解 网上貌似全部都是哈希+二分(反正我是大概baidu了翻了翻)(还有人暴力AC了的..) 哈希还是相对于dp还是比较麻烦的. 而且正确性还有可能被卡(当然这个题不会) 而且还容易写 ...
- 【矩阵哈希】【二分答案】【哈希表】bzoj1567 [JSOI2008]Blue Mary的战役地图
引用题解:http://hzwer.com/5153.html 当然,二分可以换成哈希表. #include<cstdio> #include<iostream> #inclu ...
随机推荐
- Java虚拟机_运行时数据区
Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域. 这些区域都有各自的用途.各自的创建和销毁的时间,有的区域随着虚拟机进程的启动而存在,有些区域则是依赖用户线程启动 ...
- Netty面试
声明:此文章非本人所 原创,是别人分享所得,如有知道原作者是谁可以联系本人,如有转载请加上此段话 1.BIO.NIO 和 AIO 的区别? BIO:一个连接一个线程,客户端有连接请求时服务器端就需要 ...
- 插入sql返回主键id
<insert id="insertSelective" parameterType="com.xxx.model.XDetail" useGenerat ...
- TortoiseGit用户手册
3 配置TortoiseGit 3.1 生成公钥 生成SSH安全密钥,提供给GIT版本库管理员以访问Git 版本库,点击桌面上生成的图标 然后执行执行“ssh-keygen”生成自己的公钥: 一路回车 ...
- PECL: configuration option "php_ini" is not set to php.ini location
message similar to: configuration option "php_ini" is not set to php.ini locationYou shoul ...
- IDEA下的第一个springBoot
1.第一步打开File->New->Project,SDK根据自己的需要选择,我这边选的是java7 2.Next之后 设置group 和artifact,根据自己的需要进行修改. 3.导 ...
- 推荐 VSCode 上特别好用的 Vue 插件 - vetur
作者 @octref 此前 V2EX 发过帖子,最近新增代码补全功能,综合比较应该是目前 VSCode 上面最好用的 Vue 插件. 能够实现在 .vue 文件中: 语法错误检查,包括 CSS/SCS ...
- Hive Metastore 连接报错
背景 项目中需要通过一些自定义的组件来操控hive的元数据,于是使用了remote方式来存储hive元数据,使用一个服务后台作为gateway,由它来控制hive元数据. 现象 在windows上连接 ...
- 自动补全Typeahead
采用 Typeahead (Bootstrap-3-Typeahead-master) <script type="text/javascript" src="/j ...
- opencv图像处理基础 (《OpenCV编程入门--毛星云》学习笔记一---五章)
#include <QCoreApplication> #include <opencv2/core/core.hpp> #include <opencv2/highgu ...