一开始以为是dp,后来看了一下标签。。。二分答案?之前也想过,但是没往下想,然后之后的算法就顺理成章,先求出第一个地图的所有子矩阵的hash值,然后求第二个,在上一个地图例二分查找,然后就没了。

算法很好想,也很好写,但是一开始我想的和最长公共子序列差不多的dp却不行(子矩阵是子串啊)。。。总的来说不是什么难题。

lower_bound是大于等于,返回的时候不用减一。

upper_bound是大于。

题干:

Description
Blue Mary最近迷上了玩Starcraft(星际争霸) 的RPG游戏。她正在设法寻找更多的战役地图以进一步提高自己的水平。 由于Blue Mary的技术已经达到了一定的高度,因此,对于用同一种打法能够通过的战役地图,她只需要玩一张,她就能了解这一类战役的打法,然后她就没有兴趣再玩儿这一类地图了。而网上流传的地图有很多都是属于同一种打法,因此Blue Mary需要你写一个程序,来帮助她判断哪些地图是属于同一类的。 具体来说,Blue Mary已经将战役地图编码为n*n的矩阵,矩阵的每个格子里面是一个32位(有符号)正整数。对于两个矩阵,他们的相似程度定义为他们的最大公共正方形矩阵的边长。两个矩阵的相似程度越大,这两张战役地图就越有可能是属于同一类的。
Input
第一行包含一个正整数n。 以下n行,每行包含n个正整数,表示第一张战役地图的代表矩阵。 再以下n行,每行包含n个正整数,表示第二张战役地图的代表矩阵。
Output
仅包含一行。这一行仅有一个正整数,表示这两个矩阵的相似程度。
Sample Input Sample Output HINT
样例解释:
子矩阵: 为两个地图的最大公共矩阵 约定:
n<=
Source

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = << ;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
int n,len = ;
ull mp[][],mp2[][],w[];
bool cmp(ull a,ull b)
{
return a < b;
}
void haxi(int x,int y,int l)
{
ull tot = ;
duke(i,x,x + l - )
{
duke(j,y,y + l - )
{
tot = tot * + mp[i][j];
}
}
w[++len] = tot;
}
ull haxi2(int x,int y,int l)
{
ull tot = ;
duke(i,x,x + l - )
{
duke(j,y,y + l - )
{
tot = tot * + mp2[i][j];
}
}
return tot;
}
int main()
{
read(n);
duke(i,,n)
duke(j,,n)
read(mp[i][j]);
duke(i,,n)
duke(j,,n)
read(mp2[i][j]);
int l = ,r = n;
while(l != r)
{
clean(w);
len = ;
int mid = (l + r) >> ;
duke(i,,n - mid + )
{
duke(j,,n - mid + )
{
haxi(i,j,mid);
}
}
sort(w + ,w + len + ,cmp);
int ok = ;
duke(i,,n - mid + )
{
duke(j,,n - mid + )
{
ull p = haxi2(i,j,mid);
int y = lower_bound(w + ,w + len + ,p) - w;
if(w[y] == p)
{
ok = ;
break;
}
}
if(ok == )
break;
}
if(ok == )
l = mid + ;
else
r = mid;
}
clean(w);
len = ;
duke(i,,n - l + )
{
duke(j,,n - l + )
{
haxi(i,j,l);
}
}
sort(w + ,w + len + ,cmp);
int ok = ;
duke(i,,n - l + )
{
duke(j,,n - l + )
{
ull p = haxi2(i,j,l);
int y = lower_bound(w + ,w + len + ,p) - w;
if(w[y] == p)
{
ok = ;
break;
}
}
if(ok == )
break;
}
if(ok == )
write(r);
else
write(r - );
return ;
}
/*
3
1 2 3
4 5 6
7 8 9
5 6 7
8 9 1
2 3 4
*/
/*
5
1 2 3 4 5
5 6 7 8 9
8 9 10 11 12
1 2 3 4 5
5 4 3 2 1
1 2 3 5 7
5 3 1 2 3
1 4 5 6 7
4 5 8 9 10
4 7 1 5 6
*/

B1567 [JSOI2008]Blue Mary的战役地图 二分答案+hash的更多相关文章

  1. BZOJ 1567: [JSOI2008]Blue Mary的战役地图( 二分答案 + hash )

    二分答案, 然后用哈希去判断... ------------------------------------------------------------------------- #include ...

  2. BZOJ1567 [JSOI2008]Blue Mary的战役地图 二分答案 哈希

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1567 题意概括 给出两个n*n的数字矩阵,问最大公共正方形边长. 题解 先二分答案一个m,对于每一 ...

  3. bzoj1567: [JSOI2008]Blue Mary的战役地图

    将矩阵hash.s[0]忘了弄成0,输出中间过程发现了. hash.sort.判重.大概这样子的步骤吧. #include<cstdio> #include<cstring> ...

  4. BZOJ 1567: [JSOI2008]Blue Mary的战役地图

    1567: [JSOI2008]Blue Mary的战役地图 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1011  Solved: 578[Sub ...

  5. BZOJ 1567: [JSOI2008]Blue Mary的战役地图 矩阵二维hash

    1567: [JSOI2008]Blue Mary的战役地图 Description Blue Mary最近迷上了玩Starcraft(星际争霸) 的RPG游戏.她正在设法寻找更多的战役地图以进一步提 ...

  6. [JSOI2008]Blue Mary的战役地图(二分+哈希)

    Blue Mary最近迷上了玩Starcraft(星际争霸) 的RPG游戏.她正在设法寻找更多的战役地图以进一步提高自己的水平. 由于Blue Mary的技术已经达到了一定的高度,因此,对于用同一种打 ...

  7. [JSOI2008] [BZOJ1567] Blue Mary的战役地图 解题报告 (hash)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1567 Description Blue Mary最近迷上了玩Starcraft(星际争霸 ...

  8. 【矩阵哈希】【二分答案】【哈希表】bzoj1567 [JSOI2008]Blue Mary的战役地图

    引用题解:http://hzwer.com/5153.html 当然,二分可以换成哈希表. #include<cstdio> #include<iostream> #inclu ...

  9. bzoj 1567: [JSOI2008]Blue Mary的战役地图【二分+hash】

    二维哈希+二分 说是二维,其实就是先把列hash了,然后再用列的hash值hash行,这样可以O(n)的计算一个正方形的hash值,然后二分边长,枚举左上角点的坐标然后hash判断即可 只要base选 ...

随机推荐

  1. 【sqli-labs】 less34 POST- Bypass AddSlashes (POST型绕过addslashes() 函数的宽字节注入)

    还是宽字节注入,POST版本的 uname=1&passwd=1%df' union select 1,2,3# 提交报错 列名不匹配,改一下就好了 uname=1&passwd=1% ...

  2. (转)基于MVC4+EasyUI的Web开发框架经验总结(13)--DataGrid控件实现自动适应宽带高度

    http://www.cnblogs.com/wuhuacong/p/4085725.html 在默认情况下,EasyUI的DataGrid好像都没有具备自动宽度的适应功能,一般是指定像素宽度的,但是 ...

  3. Generics of a Higher Kind

    http://adriaanm.github.io/files/higher.pdf https://www.atlassian.com/blog/archives/scala-types-of-a- ...

  4. 通过Git向Github提交代码(Windows系统)

    1.新建项目 在GitHub选择并创建一个项目.首先,登录 GitHub,单击页面右上角加号“+” ,选择“New repository” 选项. 填写项目名称及描述,默认项目为“Public”,如果 ...

  5. vue-awesome-swiper组件的使用

    一.轮播图组件是这样安装的 npm i --save-dev vue-awesome-swiper main.js里面 import 'swiper/dist/css/swiper.css' impo ...

  6. 算法87-----DAG有向无环图的拓扑排序

    一.题目:课程排表---210 课程表上有一些课,是必须有修学分的先后顺序的,必须要求在上完某些课的情况下才能上下一门.问是否有方案修完所有的课程?如果有的话请返回其中一个符合要求的路径,否则返回[] ...

  7. C#第六节课

    for循环 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...

  8. python-pycharm windows安装

    pycharm_IDE安装 1. 首先先pycharm官网,或者直接输入网址:http://www.jetbrains.com/pycharm/download/#section=windows,下载 ...

  9. Full-featured Vue 评分组件

    分享一下最近写的 vue 的评分组件 Features: 支持半星.可清除.文案展示.只读.自定义颜色.自定义字符及图片等.支持 hover 的时候改变 value.内置三种样式,以及非常好看 DEM ...

  10. openc下cv::Mat和IplImage的相互转换

    opencv2.0的类CV::Mat和opencv1.0的IplImage之间烦人转换: cv::Mat matimg = cv::imread ("girl.jpg"); Ipl ...