AtCoder Beginner Contest 176 (ABC水题,D题01BFS,E数组处理)
补题链接:Here
A - Takoyaki
很容易看出 \(\frac{N + X - 1}{X} \times T\)
B - Multiple of 9
给定一个很大的整数,问其是否是 \(9\) 的倍数
累加各个位数,然后判断取余结果
C - Step
给定一个数组,每次操作可以给一个数增加 \(1\) ,问最少多少次操作可以将数组变为不下降序列?
贪心地将当前数字加大到左边的最大值即可,如果当前数字大于最大值,则更新最大值。
D - Wizard in Maze
题意:有一个巫师在迷宫中,要从起点到终点。有两种走法:一是直接沿着路走,二是使用魔法,跳到以当前格子为中心的 \(5\times5\) 方阵中任意一个没有障碍的格子上。问最少用几次魔法,可以到终点?如果无解,输出 \(−1\)。
思路:经典 01 BFS,沿着路走的代价为 \(0\),而使用魔法的代价为 \(1\)。使用标准的 0-1BFS,也即双端队列来处理即可。
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int inf = 0x3f3f3f3f;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int h, w, sx, sy, ex, ey;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> h >> w >> sx >> sy >> ex >> ey;
sx--, sy--, ex--, ey--;
vector<string> a(h);
for (int i = 0; i < h; ++i) cin >> a[i];
vector<vector<int>> dist(h, vector<int>(w, inf));
vector<vector<bool>> vis(h, vector<bool>(w, false));
deque<pair<int, int>> q;
q.push_back({sx, sy});
dist[sx][sy] = 0;
while (q.size()) {
auto f = q.front();
q.pop_front();
int ci = f.first, cj = f.second;
if (ci == ex && cj == ey) {
cout << dist[ci][cj] << "\n";
return 0;
}
if (vis[ci][cj]) continue;
vis[ci][cj] = true;
for (int k = 0; k < 4; ++k) {
int ni = ci + dy[k], nj = cj + dx[k];
if (ni < 0 || ni >= h || nj < 0 || nj >= w ||
dist[ni][nj] <= dist[ci][cj] || a[ni][nj] == '#')
continue;
dist[ni][nj] = dist[ci][cj];
q.push_front({ni, nj});
}
for (int ni = ci - 2; ni <= ci + 2; ++ni)
for (int nj = cj - 2; nj <= cj + 2; ++nj) {
if (ni < 0 || ni >= h || nj < 0 || nj >= w || a[ni][nj] == '#' ||
dist[ni][nj] <= dist[ci][cj] + 1)
continue;
dist[ni][nj] = dist[ci][cj] + 1;
q.push_back({ni, nj});
}
}
cout << -1 << "\n";
return 0;
}
E - Bomber
有一个 \(H\times W\) 的大方阵,其中若干位置有目标物。可以设置一个炮台,炮台可以攻击同一行和同一列的所有目标。问炮台最多能攻击多少个目标?
思路:\(1 \le H,W\le 3e5\) 可想而知暴力会 TLE,
我们可以在读入的时候就维护好每行每列的目标个数,然后找到最大值的行列(可能有多个)
然后一次遍历这些点。
注意可能刚好炮塔落点的位置上有目标,然后就会重复计算一次
// Murabito-B 21/04/07
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int h, w, m;
cin >> h >> w >> m;
vector<int> hc(h + 1), wc(w + 1);
vector<pair<int, int>> p(m);
for (int i = 0; i < m; ++i) {
cin >> p[i].first >> p[i].second;
hc[p[i].first]++, wc[p[i].second]++;
}
int hm = *max_element(hc.begin(), hc.end());
int wm = *max_element(wc.begin(), wc.end());
vector<int> vh, vw;
for (int i = 1; i <= h; ++i)
if (hc[i] == hm) vh.emplace_back(i);
for (int i = 1; i <= w; ++i)
if (wc[i] == wm) vw.emplace_back(i);
int sh = vh.size(), sw = vw.size();
if (sh * sw > m) {
cout << hm + wm;
return 0;
}
set<pair<int, int>> s(p.begin(), p.end());
for (int i : vh)
for (int j : vw)
if (!s.count({i, j})) {
cout << hm + wm;
return 0;
}
cout << hm + wm - 1;
return 0;
}
F - Brave CHAIN
待补。。。
AtCoder Beginner Contest 176 (ABC水题,D题01BFS,E数组处理)的更多相关文章
- AtCoder Beginner Contest 050 ABC题
A - Addition and Subtraction Easy Time limit : 2sec / Memory limit : 256MB Score : 100 points Proble ...
- AtCoder Beginner Contest 088 (ABC)
A - Infinite Coins 题目链接:https://abc088.contest.atcoder.jp/tasks/abc088_a Time limit : 2sec / Memory ...
- AtCoder Beginner Contest 087 (ABC)
A - Buying Sweets 题目链接:https://abc087.contest.atcoder.jp/tasks/abc087_a Time limit : 2sec / Memory l ...
- AtCoder Beginner Contest 176
比赛链接:https://atcoder.jp/contests/abc176 A - Takoyaki #include <bits/stdc++.h> using namespace ...
- AtCoder Beginner Contest 176 D - Wizard in Maze (BFS,双端队列)
题意:给你一张图,"."表示能走,"#表示不能走,步行可以向四周四个方向移动一个单位,使用魔法可以移动到周围\(5\)X\(5\)的空地,问能否从起点都早终点,并求最少使 ...
- AtCoder Beginner Contest 176 E - Bomber (思维)
题意:有一张\(H\)x\(W\)的图,给你\(M\)个目标的位置,你可以在图中放置一枚炸弹,炸弹可以摧毁所在的那一行和一列,问最多可以摧毁多少目标. 题解:首先我们记录某一行和某一列目标最多的数目, ...
- AtCoder Beginner Contest 253 F - Operations on a Matrix // 树状数组
题目传送门:F - Operations on a Matrix (atcoder.jp) 题意: 给一个N*M大小的零矩阵,以及Q次操作.操作1(l,r,x):对于 [l,r] 区间内的每列都加上x ...
- AtCoder Beginner Contest 068 ABCD题
A - ABCxxx Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement This contes ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 223
AtCoder Beginner Contest 223 A是纯纯的水题,就不说了 B - String Shifting 思路分析 我真的sb,一开始想了好久是不是和全排列有关,然后读了好几遍题目也 ...
随机推荐
- 洛谷4159 [SCOI2009] 迷路(矩阵快速幂,拆点)
题意:该有向图有 n 个节点,节点从 1至 n 编号,windy 从节点 1 出发,他必须恰好在 t 时刻到达节点 n.现在给出该有向图,你能告诉 windy 总共有多少种不同的路径吗?答案对2009 ...
- redis的基本命令,并用netty操作redis(不使用springboot或者spring框架)就单纯的用netty搞。
大家如果对使用netty搞这些http请求什么的感兴趣的,可以参观我自己创建的这个项目. nanshaws/nettyWeb: 复习一下netty,并打算做一个web项目出来 (github.com) ...
- 通过 VS Code 优雅地编辑 Pod 内的代码(非 NodePort)
目录 1. 概述 2. NodePort 方式 3. Ingress 方式 4. 救命稻草 5. 其他 1. 概述 今天聊点啥呢,话说,你有没有想过怎样用 VS Code 连上 K8s 集群内的某个 ...
- GHOST 系统安装教程 轻松一键,系统恢复到最佳状态
硬盘安装系统 安装前准备 1.保证能够正常进入系统: 2.下载Ghost系统镜像文件: 3.下载镜像安装器: 安装步骤 1.下载Ghost系统镜像"Win7_x64_Pure_5.07.GH ...
- Mybatis-Flex核心功能之@Id
1.是什么? 在 Entity 类中,MyBatis-Flex 是使用 @Id 注解来标识主键的 2.怎么玩? public @interface Id { /** * ID 生成策略,默认为 non ...
- SpringBoot整合Swagger3
1.导入相关依赖 <!--swagger--> <dependency> <groupId>io.springfox</groupId> <art ...
- 蓝桥杯-最短路 (SPFA算法学习)
SPFA算法主要用来解决存在负边权的单源最短路情况(但不能有负环!!!)一个简单的方法判断是否有没有负环可以通过判断是否有一个节点是否频繁进出队列. 以下内容转自https://blog.csdn.n ...
- JXNU acm选拔赛 不安全字符串
不安全字符串 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submis ...
- CodeForces - 764C
C. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Critical error detected c0000374
我发现出现上述错误是 free 两次内存 float* dd=new float[2]; delete[] dd; delete[] dd;