时间限制: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】Min Stack(easy)

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  2. IOS- 内存管理机制

    iOS平台内存常见问题 作为iOS平台的开发者,是否曾经为内存问题而苦恼过?内存莫名的持续增长,程序莫名的crash,难以发现 的内存泄漏,这些都是iOS平台内存相关的常见问题:本文将会详细介绍iOS ...

  3. Xcode添加注释

    VVDocumenter-Xcode,自动生成注释,感觉比较方便的插件,分享下,应该很多人都知道= = 在 https://github.com/onevcat/VVDocumenter-Xcode  ...

  4. Excel计算一列的和sum(A:A)

    在公式中输入=sum(A2:A6),计算的是A列2-6行的和 =sum(A:A)计算的是A列全部的和

  5. 读取Spring的配置文件applicationContext.xml的5种方法

    1.利用ClassPathXmlApplicationContext,这种方式配置文件应该放在类包同路径下Java代码: ApplicationContext ct=new ClassPathXmlA ...

  6. Linux定时任务设定

    使用crontab 命令进行设定. 详情可参见:http://blog.csdn.net/xiyuan1999/article/details/8160977. 共有6项构成,前5项为时间:分 时 天 ...

  7. 三、jQuery--Ajax基础--Ajax全接触--扩展知识(跨域)

  8. Clr Via C#读书笔记----基元线程同步构造

    线程文章:http://www.cnblogs.com/edisonchou/p/4848131.html 重点在于多个线程同时访问,保持线程的同步. 线程同步的问题: 1,线程同步比较繁琐,而且容易 ...

  9. 12.享元模式(Flyweight Pattern)

    using System; using System.Collections; namespace ConsoleApplication5 { class Program { /// <summ ...

  10. angularjs实战

    1.指令  transclude 保留原来的内容 replace 去掉<my-directive>指令 <script src="http://apps.bdimg.com ...