10324 Global Warming dfs + 二分
时间限制: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 + 二分的更多相关文章
- 【BZOJ4149】[AMPPZ2014]Global Warming 单调栈+RMQ+二分
[BZOJ4149][AMPPZ2014]Global Warming Description 给定一个序列a[1],a[2],...,a[n].请从中选出一段连续子序列,使得该区间最小值唯一.最大值 ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- [CEOI2018]Global warming
[CEOI2018]Global warming 题目大意: 给定\(n(n\le2\times10^5)\),你可以将任意\(a_{l\sim r}(1\le l\le r\le n)\)每一个元素 ...
- BZOJ5442: [Ceoi2018]Global warming
BZOJ5442: [Ceoi2018]Global warming https://lydsy.com/JudgeOnline/problem.php?id=5442 分析: 等价于后缀加(前缀减也 ...
- Java实现 LeetCode 655 输出二叉树(DFS+二分)
655. 输出二叉树 在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则: 行数 m 应当等于给定二叉树的高度. 列数 n 应当总是奇数. 根节点的值(以字符串格式给出)应当放在可放置的第一 ...
- hdu 5188 dfs+二分
get了很多新技能 当时想到了用dfs,但是排序用的是限制时间排序,一直没搞出来. 正解: 二分用时,dfs判断,为了顺利进行做题,需要按照做题开始时间排序 还可以用dp 题意: 作为史上最强的刷子之 ...
- ACdream 1726 A Math game (dfs+二分)
http://acdream.info/problem?pid=1726 官方题解:http://acdream.info/topic?tid=4246 求n个数里面能不能选一些数出来让它们的和等于k ...
- CH 2401 - 送礼 - [折半DFS+二分]
题目链接:传送门 描述 作为惩罚,GY被遣送去帮助某神牛给女生送礼物(GY:貌似是个好差事)但是在GY看到礼物之后,他就不这么认为了.某神牛有N个礼物,且异常沉重,但是GY的力气也异常的大(-_-b) ...
- 【AtCoder Grand Contest 007E】Shik and Travel [Dfs][二分答案]
Shik and Travel Time Limit: 50 Sec Memory Limit: 512 MB Description 给定一棵n个点的树,保证一个点出度为2/0. 遍历一遍,要求每 ...
随机推荐
- 【编程题目】在从 1 到 n 的正数中 1 出现的次数
30.在从 1 到 n 的正数中 1 出现的次数(数组)题目:输入一个整数 n,求从 1 到 n 这 n 个整数的十进制表示中 1 出现的次数.例如输入 12,从 1 到 12 这些整数中包含 1 的 ...
- IOS-MVC的使用
1.Model不允许和Controller,View打交道.也就是Model根本不知道谁会用自己,Model中不能有任何对 Controller和View的引用.正所谓:Don't call me, ...
- 如何将Js代码封装成Jquery插件
很多相同的Jquery代码会在很多页面使用,每次都复制粘贴太麻烦了,不如封装成一个Jquery插件就方便了,至于影响网页的速度不,我就没有测试了哈. 代码如下 这是一个自定闪烁打印文字的Jquery特 ...
- 火狐----此地址使用了一个通常用于网络浏览以外的端口。出于安全原因,Firefox 取消了该请求。
FirFox打开80以外的端口,会弹出以下提示: “此地址使用了一个通常用于网络浏览以外的端口.出于安全原因,Firefox 取消了该请求.”.经网上搜索,解决方法如下: 在Firefox地址栏输入a ...
- August 22nd 2016 Week 35th Monday
Have you ever given any thought to your future? 你有没有为将来打算过呢? Have you ever given any thought to your ...
- 阻塞队列BlockingQueue 学习
import java.util.Random; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Time ...
- EVE ToDo
1. 打捞无人机 2. 无人机命中
- RST_n的问题
有一个灰常郁闷的问题... module CLK_Generater( input CLOCK_100, i ...
- windows服务 2.实时刷新App.config
参考 http://www.cnblogs.com/jeffwongishandsome/archive/2011/04/24/2026381.html http://www.cnblogs.com/ ...
- [LeetCode] Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...