http://219.244.176.199/JudgeOnline/problem.php?id=1215

这是这次微软和百度实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是不严格递增的,而且每一列都是不严格递增。给你一个数k,请你判断k是否存在于矩阵中。

微软面试的时候没有想到很好的做法,后来想了好久,发现就是一个在矩形上的二分。对于一个矩形,我选取中间一列mid列二分,那么能得到一个index,如果a[index][mid]不为k,那么index以上的都小于k,index以下的都大于k。根据行和列都递增,那么mid列之前且index之上都小于k,在mid列之后且index之下的都大于k。于是这个矩形的空间就可以缩成mid列之后且index之上和在mid列之前且index之下的两块区域,而且两块区域的和为原矩形的一般。如果递归下去就可以解决了。时间复杂度log(nm)*logn

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long using namespace std; const int maxN = ;
int a[maxN][maxN];
int n, m, q; void input()
{
for (int i = ; i < n; ++i)
for (int j = ; j < m; ++j)
scanf("%d", &a[i][j]);
} int binSearch(int col, int from, int to, int k)
{
int lt = from, rt = to, mid;
while (lt+ < rt)
{
mid = (lt+rt)>>;
if (a[mid][col] <= k) lt = mid;
else rt = mid;
}
if (a[rt][col] <= k) return rt;
else return lt;
} bool dfs(int fromx, int fromy, int tox, int toy, int k)
{
if (fromx >= n || fromy >= m || tox < || toy < ) return false;
if (fromx > tox || fromy > toy) return false;
int mid, index;
bool ans = false;
mid = (fromy+toy)>>;
index = binSearch(mid, fromx, tox, k);
if (a[index][mid] == k) return true;
else if (a[index][mid] > k) index--;
ans = ans || dfs(fromx, mid+, index, toy, k);
ans = ans || dfs(index+, fromy, tox, mid-, k);
return ans;
} void work()
{
int k;
scanf("%d", &q);
for (int i = ; i < q; ++i)
{
scanf("%d", &k);
if (dfs(, , n-, m-, k)) printf("Yes\n");
else printf("No\n");
}
} int main()
{
//freopen("test6.in", "r", stdin);
//freopen("test6.out", "w", stdout);
while (scanf("%d%d", &n, &m) != EOF)
{
input();
work();
} return ;
}

ACM学习历程—SNNUOJ1215 矩阵2(二分 && dfs)的更多相关文章

  1. ACM学习历程—SNNUOJ1214 矩阵1(二分)

    题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1214 这是这次微软实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是 ...

  2. ACM学习历程—HDU1716 排列2(dfs && set容器)

    Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数.   Input 每组数据占一行,代表四张卡片上的数字( ...

  3. ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目大意就是构造一个行列和每个角的2*2都是1234的4*4矩阵. 用dfs暴力搜索,不过需要每一步进 ...

  4. ACM学习历程—HDU5587 Array(数学 && 二分 && 记忆化 || 数位DP)(BestCoder Round #64 (div.2) 1003)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后 ...

  5. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  6. ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)

    http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...

  7. ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...

  8. ACM学习历程—UESTC 1215 Secrete Master Plan(矩阵旋转)(2015CCPC A)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1215 题目大意就是问一个2*2的矩阵能否通过旋转得到另一个. 代码: #include <iostre ...

  9. ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1219 题目大意是给了一张图,然后要求一个点通过路径回到这个点,使得xor和最大. 这是CCPC南阳站的一道题 ...

随机推荐

  1. OpenGL学习进程(3)第一课:初始化窗体

        本节是OpenGL学习的第一个课时,下面介绍如何初始化一个窗体:     (1)显示一个有蓝色背景的窗体: #include <GL/glut.h> #include <st ...

  2. Windows虚拟机安装Linux系统

    windows系统安装linux centos虚拟系统 1.下载 VMware Workstation Pro并安装,效果如图 2.下载linux系统 https://www.centos.org/d ...

  3. bex5部署后不更新

    哪个模块没更新,就编译哪个模块 在x5/tools/compile下,运行对应模块的bat,并清空浏览器缓存 如果修改了.w文件,也可以删除相应的.catch文件夹 和.release文件夹,并且注意 ...

  4. LVS 介绍

    LVS 介绍 说明: LVS是Linux Virtual Server的简称 LVS是一个实现负载均衡的开源软件项目 LVS效率要高于Nginx LVS工作在ISO的第4层(传输层) LVS架构有三层 ...

  5. POI 百万数据导出

    poi 导出主类 package test; import java.io.File; import java.io.FileOutputStream; import java.lang.reflec ...

  6. linux上安装程序出现的问题汇总

    1.程序在编译过程中出现:variable set but not used [-Werror=unused-but-set-variable] 解决方法:将configure文件和Makefile文 ...

  7. iOS应用网络安全之HTTPS

    移动互联网开发中iOS应用的网络安全问题往往被大部分开发者忽略,iOS9和OS X 10.11开始Apple也默认提高了安全配置和要求.本文以iOS平台App开发中对后台数据接口的安全通信进行解析和加 ...

  8. RequestMapping请求映射方式

    1.标准映射 规则: 1) @RequestMapping可以设置在类上,也可以设置在方法上 2) 请求的映射规则是:类上的RequestMapping + 方法上的RequestMapping 3) ...

  9. Luogu-1527 [国家集训队]矩阵乘法

    Luogu-1527 [国家集训队]矩阵乘法 题面 Luogu-1527 题解 昨天学CDQ分治时做了一些题,但是因为题(wo)太(tai)水(lan)了(le)并没有整理 学了一晚上的整体二分,拿这 ...

  10. mysql基础(4)-数据导入

    如何把数据导入(出)mysql 导出 sql语句         select * from 表名 into outfile "详细路径" fields terminated by ...