时间限制:1000MS  内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题   语言: G++;GCC

Description

Global warming is a big problem, isn't it? It is reported that the ice of Antarctica is melting. It results that oceans are glowing
and more and more places are flooded. Now the city of CODE and the city of ACM are in front of the problem though no wather seperate
the two cities. The situation will change with the ice continue melting. It is reported that the oceans glow P cm per year. The mayors
of the two cities want to know how many yeas there is before the two cities are seperated by water.
The map can be descriped as following

It is a M*N blocks
The city of CODE locates in the block of coordinate(1,1), the city of ACM locates in the block of coordinate(M,N). The altitude of
every blocks are given. It is guaranteed that the block of (1,1) and the block of (M,N) is higher than their neighbor block.
The following map shows the two cities weren't seperated by water.

The following map shows the two cities are eperated by water.

输入格式

The first line contains the two integer numbers N (1 <= N <= 500), M (1 <= M <= 500). Then N lines are following.
Each line contains
M integer numbers(seperated by a space). Each number no more than 1,000,000,000). The last line contains the integer number
P (1 <= P <= 1,000,000,000)

输出格式

The only line of the output contains the years mentioned above.

输入样例

3 3
9 5 7
4 6 8
8 3 9
1

输出样例

6

思路:以图中的最低海拔作为low,最高海拔作为high,二分一个值k,该值为能淹没部分区域并且把(1,1)和(n,m) 隔开的下界,那么答案即为 k/p 取上界。

其中用到dfs判断(1,1)和(n,m)两个点是否连通

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std ;
typedef long long ll ;
const int INF = << ;
ll r[][] ;
int vis[][] ;
int dir[][] = {{-, },{, -},{, },{, }} ;
ll n, m, mx, mi, p ;
void dfs(int i, int j)
{
vis[i][j] = ;
for(int k = ; k < ; ++k)
{
int ti = i + dir[k][] ;
int tj = j + dir[k][] ;
if(ti < || ti > n || tj < || tj > m) continue ;
if(vis[ti][tj]) continue ;
dfs(ti, tj) ;
}
}
int go(int t)
{
memset(vis, , sizeof vis) ;
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j)
if(r[i][j] < t) vis[i][j] = ;
dfs(,) ;
if(vis[n][m]) return ;
else return ;
}
int solve()
{
int low = mi, high = mx ;
while(low < high)//二分
{ int m = (low + high) >> ;
//printf("[%d] ",m) ;
if(go(m)) high = m ;
else low = m + ;
}
return low ;
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin) ;
#endif
mi = INF ; mx = -INF ;
scanf("%d%d",&n,&m) ;
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j){
scanf("%lld",&r[i][j]) ;
if(r[i][j] > mx) mx = r[i][j] ;
else if(r[i][j] < mi) mi = r[i][j] ;
}
// printf("%d %d\n",mi,mx) ;
scanf("%lld",&p) ;
int ans ;
ans = solve() ;
// printf("%d\n",ans) ;
if(ans % p == ) ans = ans / p ;
else ans = ans / p + ;
printf("%d\n",ans) ;
}

10324 Global Warming dfs + 二分的更多相关文章

  1. 【BZOJ4149】[AMPPZ2014]Global Warming 单调栈+RMQ+二分

    [BZOJ4149][AMPPZ2014]Global Warming Description 给定一个序列a[1],a[2],...,a[n].请从中选出一段连续子序列,使得该区间最小值唯一.最大值 ...

  2. uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)

    Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...

  3. [CEOI2018]Global warming

    [CEOI2018]Global warming 题目大意: 给定\(n(n\le2\times10^5)\),你可以将任意\(a_{l\sim r}(1\le l\le r\le n)\)每一个元素 ...

  4. BZOJ5442: [Ceoi2018]Global warming

    BZOJ5442: [Ceoi2018]Global warming https://lydsy.com/JudgeOnline/problem.php?id=5442 分析: 等价于后缀加(前缀减也 ...

  5. Java实现 LeetCode 655 输出二叉树(DFS+二分)

    655. 输出二叉树 在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则: 行数 m 应当等于给定二叉树的高度. 列数 n 应当总是奇数. 根节点的值(以字符串格式给出)应当放在可放置的第一 ...

  6. hdu 5188 dfs+二分

    get了很多新技能 当时想到了用dfs,但是排序用的是限制时间排序,一直没搞出来. 正解: 二分用时,dfs判断,为了顺利进行做题,需要按照做题开始时间排序 还可以用dp 题意: 作为史上最强的刷子之 ...

  7. ACdream 1726 A Math game (dfs+二分)

    http://acdream.info/problem?pid=1726 官方题解:http://acdream.info/topic?tid=4246 求n个数里面能不能选一些数出来让它们的和等于k ...

  8. CH 2401 - 送礼 - [折半DFS+二分]

    题目链接:传送门 描述 作为惩罚,GY被遣送去帮助某神牛给女生送礼物(GY:貌似是个好差事)但是在GY看到礼物之后,他就不这么认为了.某神牛有N个礼物,且异常沉重,但是GY的力气也异常的大(-_-b) ...

  9. 【AtCoder Grand Contest 007E】Shik and Travel [Dfs][二分答案]

    Shik and Travel Time Limit: 50 Sec  Memory Limit: 512 MB Description 给定一棵n个点的树,保证一个点出度为2/0. 遍历一遍,要求每 ...

随机推荐

  1. 【leetcode】Jump Game I & II (hard)

    Jump Game (middle) Given an array of non-negative integers, you are initially positioned at the firs ...

  2. IOS - NSURLSession

    NSURLSession是iOS7中新的网络接口,它与咱们熟悉的NSURLConnection是并列的.在程序在前台时,NSURLSession与NSURLConnection可以互为替代工作.注意, ...

  3. Ajax如何使用Session

    在Ajax中有时会使用到Session,在aspx.cs文件这样获取: string name = Session["name"]; 但是在Ajax中就不能这样获取Session, ...

  4. 统计 F-test 和 T-test

    1 显著性差异 如果样本足够大,很容易有显著性差异.样本小,要有显著性差异很难. y是因变量,x是自变量 2 F-test与T-test Ftest也称ANOVA,是用来检测一个y下的不同level的 ...

  5. iOS 语录

    1. 输入法切换: cmd + space 2. xcode 退出全屏control + cmd + f 3. xcode 代码格式化插件Uncrustify,XAlign, CLangFormat ...

  6. AJAX 汽车详细信息练习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. python 连接sql server

    linux 下pymssql模块的安装 所需压缩包:pymssql-2.1.0.tar.bz2freetds-patched.tar.gz 安装: tar -xvf pymssql-2.1.0.tar ...

  8. CLR via C#(09)-扩展方法

    对于一些现成的类,如果我们想添加一些新的方法来完善功能,但是不想改变已有的封装,也不想使用派生类,那么该怎么办呢?这里我们可以使用扩展方法. 一见钟情--初识扩展 扩展方法使您能够向现有类型“添加”方 ...

  9. svn不知道这样的主机

    重做服务器后,计算机名称肯定是不一样的.我们之前的项目还是老计算机名字,只要更改一下计算机名字即可实现.或者更改

  10. android 消息推送

    android 消息推送 极光推送百度云推送(语音)友盟消息推送