AtCoder Beginner Contest 170
比赛链接:https://atcoder.jp/contests/abc170
A - Five Variables
题意
$5$ 个数中有 $1$ 个 $0$,判断是第几个。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
int x; cin >> x;
if (x == 0) {
cout << i << "\n";
return 0;
}
}
}
B - Crane and Turtle
题意
判断是否可能共有 $x$ 只鹤龟,且二者腿数加起来共有 $y$ 条。($1 \le x \le 100, 1 \le y \le 100$)
题解一
枚举二者的数量,时间复杂度 $O_{(n^2)}$ 。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, y; cin >> x >> y;
for (int i = 0; i <= 100; i++) {
for (int j = 0; j <= 100; j++) {
if (i + j == x and 2 * i + 4 * j == y) {
cout << "Yes" << "\n";
return 0;
}
}
}
cout << "No" << "\n";
}
题解二
枚举一者的数量,时间复杂度 $O_{(n)}$ 。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, y; cin >> x >> y;
for (int i = 0; i <= x; i++) {
int j = x - i;
if (2 * i + 4 * j == y) {
cout << "Yes" << "\n";
return 0;
}
}
cout << "No" << "\n";
}
题解三
如果 $y$ 在 $[2x, 4x]$ 之间且 $y$ 为偶数则一定有解,时间复杂度 $O_{(1)}$ 。
证明
鹤龟共有 $x$ 只,假设初始时均为龟,则有 $2x$ 条腿,之后每次可用一只鹤换下一只龟,腿的条数增加 $2$ 条,最多增加 $2x$ 条腿,即至 $4x$,所以 $[2x,4x]$ 间的偶数都是可行的。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, y; cin >> x >> y;
cout << (2 * x <= y and y <= 4 * x and y % 2 == 0 ? "Yes" : "No");
}
C - Forbidden List
题意
找出整数集中除 $n$ 个数外与 $x$ 绝对值之差最小的数。($0 \le n \le 100,1 \le p_i \le 100, 1 \le x \le 100$)
题解一
只需构造 $[0,101]$ 即可,每次查询的时间复杂度为 $O_{(n)}$ 。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, n; cin >> x >> n;
set<int> st;
for (int i = 0; i <= 101; i++)
st.insert(i);
for (int i = 0; i < n; i++) {
int t; cin >> t;
st.erase(t);
}
int ans = 0;
int mi = INT_MAX;
for (auto i : st)
if (abs(i - x) < mi)
mi = abs(i - x), ans = i;
cout << ans << "\n";
}
题解二
每次查询的时间复杂度为 $O_{(logn)}$ 。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, n; cin >> x >> n;
set<int> st;
for (int i = 0; i <= 101; i++)
st.insert(i);
for (int i = 0; i < n; i++) {
int t; cin >> t;
st.erase(t);
}
auto it = st.lower_bound(x);
int abs1 = abs(*it - x);
int abs2 = INT_MAX;
if (it != st.begin())
abs2 = abs(*(--it) - x);
cout << (abs1 < abs2 ? x + abs1 : x - abs2);
}
D - Not Divisible
题意
找出 $n$ 个数中有多少个数不被其他数整除。
题解
排序后从小到大筛即可,需要标记已访问元素来优化。
代码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6; set<int> st;
map<int, bool> repeat, vis; int main() {
int n; cin >> n;
for (int i = 0; i < n; i++) {
int x; cin >> x;
if (st.count(x)) repeat[x] = true;
st.insert(x);
}
int ans = 0;
for (auto i : st) {
if (vis[i]) continue; //重要的优化
for (int j = i + i; j <= MAXN; j += i)
vis[j] = true;
ans += !repeat[i] and !vis[i];
}
cout << ans << "\n";
}
E - Smart Infants
题意
给出 $n$ 个人的分数和初始组别,接下来有 $q$ 个人的调遣,输出每次调遣后所有组最大分数中的最小值。
题解
维护一个最大值集合,需要更新的两种情况:
- 对于原集合,如果删除元素后为空或余下最大值小于删除元素,更新该集合的最大值。
- 对于新集合,如果插入元素前为空或已有最大值小于插入元素,更新该集合的最大值。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10; int a[N]; //第i个人的分数
int mp[N]; //第i个人的组别
multiset<int> st[N]; //N个组
multiset<int> st_mx; //最大值集合 int main() {
int n, q; cin >> n >> q;
for (int i = 1; i <= n; i++) {
int a_i, b; cin >> a_i >> b;
st[b].insert(a_i);
mp[i] = b, a[i] = a_i;
}
for (int i = 0; i < N; i++)
if (st[i].size())
st_mx.insert(*st[i].rbegin());
for (int i = 0; i < q; i++) {
int c, d; cin >> c >> d;
st[mp[c]].erase(st[mp[c]].find(a[c]));
if (st[mp[c]].size() == 0) {
st_mx.erase(st_mx.find(a[c]));
} else {
if (*st[mp[c]].rbegin() < a[c]) {
st_mx.erase(st_mx.find(a[c]));
st_mx.insert(*st[mp[c]].rbegin());
}
}
if (st[d].size() == 0 or a[c] > *st[d].rbegin()) {
if (st[d].size())
st_mx.erase(st_mx.find(*st[d].rbegin()));
st_mx.insert(a[c]);
}
st[d].insert(a[c]);
mp[c] = d;
cout << *st_mx.begin() << "\n";
}
}
F - Pond Skater
题意
有一个 $h \times w$ 的迷宫,每次移动最多向同一个方向连续走 $k$ 步,问从 $(x_1, y_1)$ 到 $(x_2, y_2)$ 最少需要移动多少次。
题解
$bfs$ 模拟。
代码
#include <bits/stdc++.h>
using namespace std;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9; int main() {
int h, w, k, x1, y1, x2, y2; cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
--x1, --y1, --x2, --y2;
char MP[h][w] = {};
int dis[h][w] = {};
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> MP[i][j], dis[i][j] = INF;
queue<pair<int, int>> que;
dis[x1][y1] = 0;
que.push({x1, y1});
while (!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();
for (int i = 0; i < 4; i++) {
int nx = x, ny = y;
int nd = dis[x][y] + 1;
for (int j = 1; j <= k; j++) {
nx += dir[i][0];
ny += dir[i][1];
if (nx < 0 or ny < 0 or nx >= h or ny >= w or MP[nx][ny] == '@' or dis[nx][ny] < nd)
break;
if (dis[nx][ny] > nd) {
dis[nx][ny] = nd;
que.push({nx, ny});
}
}
}
}
int ans = dis[x2][y2];
cout << (ans == INF ? -1 : ans);
}
AtCoder Beginner Contest 170的更多相关文章
- AtCoder Beginner Contest 170 D - Not Divisible (数学)
题意:有一长度为\(n\)的数组,求该数组中有多少元素不能整除其它任一元素的个数. 题解:刚开始写了个分解质因数(我是傻逼),后来发现直接暴力枚举因子即可,注意某个元素出现多次时肯定不满足情况,再特判 ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
- AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】
AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...
- AtCoder Beginner Contest 064 D - Insertion
AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...
随机推荐
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...
- 【JS学习】String基础方法
前言:本博客系列为学习后盾人js教程过程中的记录与产出,如果对你有帮助,欢迎关注,点赞,分享.不足之处也欢迎指正,作者会积极思考与改正. 目录 定义: 字符串的连接: 标签模板的使用: 字符串的基本方 ...
- 攻防世界 - Web(三)
PHP2: 1.进入页面,进行抓包或后台扫描都没有什么发现,然后网上查一波wp,发现是关于.phps文件,进入index.phps,弹出一段代码,查看源代码, <?php if("ad ...
- ASP.NET MVC--sqlserver数据库脚本的导入导出
1.右键选择数据库---任务----生成脚本 2.弹出如下框 导出整个表,默认下一步,否则选择特定数据库对象表单选框 3.修改文件名路径,可以保存脚本到制定路径,否则为默认,点击高级进入 要编写脚本的 ...
- perl打开本地/服务器图片
index.html <html> <body> <h2> perl read img </h2> <img src = "displa ...
- winform 扫码识别二维码
因为公司业务需求,需要在Windows系统下调用摄像头识别二维码需求,就有了这个功能. 我根据网上网友提供的一些资料,自己整合应用到项目中,效果还不错(就是感觉像素不是太好) 现在将调用摄像头+识别二 ...
- ftp上传文件出现553 Could not creat files 严重文件传输错误
之前上传文件到云服务器上一直出错,发现可以下载但是不能上传和编辑,后来终于找到原因了,是因为上传文件所在文件夹默认只有root用户才有写权限,所以我们还要将写权限赋予给其他用户.可以用Xshell 5 ...
- LVS负载均衡之DR模式原理介绍
LVS基本原理 流程解释: 当用户向负载均衡调度器(Director Server)发起请求,调度器将请求发往至内核空间 PREROUTING 链首先会接收到用户请求,判断目标 IP 确定是本机 IP ...
- RabbitMq消费者在初始配置之后进行数据消费
RabbitMq消费者在初始配置之后进行数据消费 问题背景 在写一个消费rabbitmq消息的程序是,发现了一个问题,消费者的业务逻辑里面依赖这一些配置信息,但是当项目启动时,如果队列里面有积压数据的 ...
- Golang拼接字符串的5种方法及其效率_Chrispink-CSDN博客_golang 字符串拼接效率 https://blog.csdn.net/m0_37422289/article/details/103362740
Different ways to concatenate two strings in Golang - GeeksforGeeks https://www.geeksforgeeks.org/di ...