牛客OI周赛8-提高组A-用水填坑
题目
链接:
https://ac.nowcoder.com/acm/contest/403/A
来源:牛客网
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
现有一块nm的地,每块11的地的高度为hi,j,在n*m的土地之外的土地高度均为0(即你可以认为最外圈无法积水)
现在下了一场足够大的雨,试求出这块地总共积了多少单位体积的水
输入描述:
第一行两个数 n, m,具体含义见题意
接下来 n 行每行 m 个数, 第 i 行为 hi,1, hi,2, ..., hi,m
输出描述:
输出一行一个数表示积水的总量
示例1
输入
复制
5 5
0 0 5 0 0
0 4 3 8 2
9 5 7 2 7
1 9 6 5 4
1 0 0 6 2
输出
复制
4
示例2
输入
复制
10 10
0 0 0 0 0 0 0 0 0 0
0 5 2 6 4 3 1 7 8 0
0 6 4 2 3 5 1 4 6 0
0 3 6 4 1 2 4 7 8 0
0 2 5 5 1 2 3 4 4 0
0 2 3 1 5 4 4 1 4 0
0 4 1 2 3 4 5 2 1 0
0 7 5 5 1 5 4 5 7 0
0 1 3 5 5 4 6 8 7 0
0 0 0 0 0 0 0 0 0 0
输出
复制
23
备注:
- 对于10%的数据, n, m ≤ 4, h ≤ 5;
- 对于前50%的数据, n, m ≤ 20, h ≤ 10;
- 对于前70%的数据, n, m ≤ 100, h ≤ 50;
- 对于另外10%的数据, 不存在两个连续的块能积水;
- 对于95%的数据, n, m ≤ 500, h ≤ 100.
- 对于100%的数据, n, m ≤ 1000, h ≤ 1000,000,000.
思路
用优先队列bfs,从图中最低的点开始bfs,这样就能从最低的高度开始搜了。
代码
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 1005;
const int MOD = 1e9 + 9;
const double pi = 3.1415926;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
struct P
{
int x, y;
LL num;
bool operator < (const P &a) const
{
return num > a.num;
}
};
priority_queue<P> p;
int n, m, vis[1005][1005];
LL mp[N][N], h[N][N];
int dis[][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
int main()
{
cin >> n >> m;
for(int i = 1;i <= n;++i)
{
for(int j = 1;j <= m;++j)
{
cin >> mp[i][j];
if(i == 1 || j == 1 || i == n || j == m)
{
vis[i][j] = 1;
h[i][j] = mp[i][j];
p.push((P){i, j, mp[i][j]});
}
}
}
while(!p.empty())
{
P now = p.top();
p.pop();
for(int i = 0;i < 4;++i)
{
int x = now.x + dis[i][0];
int y = now.y + dis[i][1];
if(x < 1 || y < 1 || x > n || y > m || vis[x][y])
continue;
h[x][y] = max(mp[x][y], now.num);
vis[x][y] = 1;
p.push((P){x, y, h[x][y]});
}
}
LL ans = 0;
for(int i = 1;i <= n;++i)
for(int j = 1;j <= m;++j)
ans += h[i][j] - mp[i][j];
cout << ans << endl;
return 0;
}
牛客OI周赛8-提高组A-用水填坑的更多相关文章
- 牛客OI周赛9-提高组题目记录
牛客OI周赛9-提高组题目记录 昨天晚上做了这一套比赛,觉得题目质量挺高,而且有一些非常有趣而且非常清奇的脑回路在里边,于是记录在此. T1: 扫雷 题目链接 设 \(f_i\) 表示扫到第 \(i\ ...
- 牛客OI周赛2-提高组
A.游戏 链接:https://www.nowcoder.com/acm/contest/210/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语 ...
- 牛客OI周赛11-普及组 B Game with numbers (数学,预处理真因子)
链接:https://ac.nowcoder.com/acm/contest/942/B 来源:牛客网 Game with numbers 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C+ ...
- 牛客OI周赛7-提高组 A 小睿睿的等式
链接:https://ac.nowcoder.com/acm/contest/371/A来源:牛客网 小睿睿在游戏开始时有n根火柴棒,他想知道能摆成形如“A+B=n”的等式且使用的火柴棒数也恰好等于n ...
- 牛客OI周赛7-提高组 B小睿睿的询问(ST打表)
链接:https://ac.nowcoder.com/acm/contest/371/B来源:牛客网 小睿睿的n个妹纸排成一排,每个妹纸有一个颜值val[i].有m个询问,对于每一个询问,小睿睿想知道 ...
- 牛客OI周赛7-普及组 解题报告
出题人好评. 评测机差评. A 救救喵咪 二位偏序.如果数据范围大的话直接树状数组,不过才1000就\(O(n^2)\)暴力就ok了. #include <bits/stdc++.h> s ...
- 牛客OI周赛10-普及组-A眼花缭乱的街市-(加速+二分)
https://ac.nowcoder.com/acm/contest/901/A 很简单的一道题,全场只有20+AC,卡时间.新学了cin加速语法和数组二分查找的函数调用. 知道有个读写挂,可以加速 ...
- 牛客OI周赛8-普及组
https://ac.nowcoder.com/acm/contest/543#question A. 代码: #include <bits/stdc++.h> using namespa ...
- 牛客OI周赛7-提高组
https://ac.nowcoder.com/acm/contest/371#question A.小睿睿的等式 #include <bits/stdc++.h> using names ...
随机推荐
- jquery怎么根据后台传过来的值动态设置下拉框、单选框选中
$(function(){ var sex=$("#sex").val(); var marriageStatus=$("#marriageStatus").v ...
- jQuery框架-3.jQuery自定义封装插件和第三方插件
一.jQuery的封装扩展 1.jQuery中extend方法使用 (挂在到jQuery和jQuery.fn两对象身上的使用) 1.1.官方文档定义: jQuery.extend Merge th ...
- SCI EI期刊
coming soon 关键字:Computer Vision, Computing, Image, Intelligence, IEEE, Compution <Journal of Expe ...
- [转]快速入门系列--WebAPI--01基础
本文转自:http://www.cnblogs.com/wanliwang01/p/aspnet_webapi_base01.html ASP.NET MVC和WebAPI已经是.NET Web部分的 ...
- [转]分布式中使用Redis实现Session共享(二)
本文转自:http://www.cnblogs.com/yanweidie/p/4678095.html 上一篇介绍了一些redis的安装及使用步骤,本篇开始将介绍redis的实际应用场景,先从最常见 ...
- EBS常用接口表
AP接口表: AP_INVOICES_INTERFACE AP_INVOICE_LINES_INTERFACE 涉及的请求: 应付款管理系统开放接口导入 涉及案例: 运费导AP.费用导AP PO接口表 ...
- [leetcode] 11. Same Tree
因为我刷题是难度不是按发布日期,所以就有可能遇到这种情况,比如这个... Given two binary trees, write a function to check if they are e ...
- jenkin+docker+git持续集成环境搭建
1.安装Jenkins(需要在Jenkins容器中安装maven,java环境不用安装,Jenkins初次启动时会自动安装) 参考:docker中安装Jenkins 2.配置git 3.安装docke ...
- django:multivaluedictkeyerror错误
查了一下,是因为获取前台数据时,用了request.POST[],改用request.POST.get()之后没有这个报错了 细节: request.POST是用来接受从前端表单中传过来的数据,比如用 ...
- Pi 在Windows下面使用远程桌面登录
1.删除系统自带的xrdp 输入命令sudo apt-get purge xrdp pi@raspberrypi:~ $ sudo apt-get purge xrdp 正在读取软件包列表... 完成 ...