牛客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 ...
随机推荐
- swift 动画
// // ViewController.swift // Anamation // // Created by su on 15/12/9. // Copyright © 2015年 tia ...
- [label][git-commands] Several Git Commands
The process of Git commands Operation 1. git commit -m 'fist post' Windows PowerShellCopyright (C) 2 ...
- long polling
Regular http: client 发出请求到server server 计算 response server 响应 response 给 client Polling: A client re ...
- Js复制链接代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- mvc和mvvm的区别?
源自:https://segmentfault.com/q/1010000000534091?_ea=178721 Model:很简单,就是业务逻辑相关的数据对象,通常从数据库映射而来,我们可以说是与 ...
- Load sharepoint envirement by powershell
#判断当前上下文环境中是否装在了SharePoint的Powershell环境,如果没有装载,则装载到当前运行环境.$Snapin = get-PSSnapin | Where-Object {$_. ...
- iOS 应用隐藏状态栏
有时候在 Storyboard 和 target 里的设定并不足以使得应用能够完全隐藏状态栏. 这时候需要到 Info.plist,添加一项:View controller-based status ...
- 关于IDENTITY_INSERT的用法介绍
IDENTITY_INSERT用于对表中的标识列进行显式插入操作时的设置.格式如下: set identity_insert TABLE_NAME ON/OFF 如果需要对表中定义为IDENTITY属 ...
- deepin配置反向代理映射本地到公网
这里我是用的小米球的免费ngrok 相信deepin的新用户在配置反向代理时,会感觉到一脸茫然,因为一开始我也是这样,但经过短暂的了解了deepin后,发现,其实与在Debian上配置并没有什么区别! ...
- 【招聘】滴滴滴~ i春秋内推直通车来咯,帮你找工作!
凑是这么简单粗暴,i春秋冬日特享福利!虽然金九银十已经过去,但素想换工作想找工作的小哥哥小姐姐看过来! [职位方向]渗 透 测 试.代 码 审 计.安全开发.病毒分析.风险控制.安全运维.....任何 ...