牛客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 ...
随机推荐
- SVN下的文件被locked不能update和commit
一次早上开机svn更新代码,提示某一个文件夹被locked,提示clean up,但是clearn up也不行,get lock不行,release lock不行 根据网上搜的,.svn(隐藏文件夹) ...
- 网格去噪 Mesh Denoising Guided by Patch Normal Co-filtering via Kernel Low-rank Recovery
http://staff.ustc.edu.cn/~lgliu/ 网格去噪 https://blog.csdn.net/lafengxiaoyu/article/details/73656060
- Photoshop 原画绘制
... <伯里曼> 手绘.鼠绘和板绘.
- Hdu1547 Bubble Shooter 2017-01-20 18:38 44人阅读 评论(0) 收藏
Bubble Shooter Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- 6、Docker Image
6.1 什么是image 文件和meta data的集合(root filesystem) 分层的,并且每一层都可以添加.改变.删除文件,成为一个新的image 不同的image可以共享相同的laye ...
- SQL -- What Tables Queries are Used to Display the Counts in the Inventory Account Periods form (INVTTGPM.fmb) (Doc ID ID 357997.1)
Applies to: Oracle Inventory Management - Version 11.5.9 to 12.1.3 [Release 11.5 to 12.1] Informatio ...
- DataTable转List<T>
/// <summary> /// DataTable转List<T> /// </summary> public static class TableToList ...
- SQL Union 和Union All 的区别
Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并 ...
- 多彩浏览器win10版 隐私声明
(一)隐私保护 the app need internet access,we won't need your private information, in other words, your i ...
- API接口安全加强设计方法
前面两篇相关文章: <Web Api 内部数据思考 和 利用http缓存优化 Api> <Web Api 端点设计 与 Oauth> 1.开放的接口 这样的接口我们天天都在接触 ...