比赛链接

A

题解

知识点:贪心。

注意到 \(m\geq n\) 时,不存在某一行或列空着,于是不能移动。

而 \(m<n\) 时,一定存在,可以移动。

时间复杂度 \(O(1)\)

空间复杂度 \(O(1)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; bool solve() {
int n, m;
cin >> n >> m;
for (int i = 1;i <= m;i++) {
int x, y;
cin >> x >> y;
}
if (m >= n) return false;
else cout << "YES" << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << "NO" << '\n';
}
return 0;
}

B

题解

知识点:贪心。

每次干掉两端 \(b\) 最小的即可,能保证最大的 \(b\) 没有增加花费,其他 \(b\) 只增加花费一次。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int a[200007], b[200007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= n;i++) cin >> b[i]; ll sum = 0;
for (int i = 1;i <= n;i++) sum += a[i];
int l = 1, r = n;
while (l < r) {
if (b[l] <= b[r]) sum += b[l++];
else sum += b[r--];
}
cout << sum << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

C

题解

知识点:博弈论,贪心,二分。

本来数据范围能暴力,但执着找规律推结论,结果推假了wwwwwwwwww,不如直接暴力QAQ。

显然二分 \(k\) 可以,$k \in[1,\lceil \frac{n}{2} \rceil] $。二者选取的贪心策略也很明显,A尽量取大的,B取最小的,推到这一步可以直接模拟了。

但进一步可以推出,A取后 \(k\) 个之后,B一定取了前 \(k-1\) 个,那么我们把前 \(k-1\) 个空出来,让A直接从 \(k\) 开始取是最优的,正着取的第 \(i\) 个是第 \(k-i+1\) 回合,只要小于等于 \(i\) 即可。

时间复杂度 \(O(n \log n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int n;
int a[107];
bool check(int mid) {
bool ok = 1;
for (int i = 1;i <= mid;i++) {
ok &= a[mid + i - 1] <= i;
}
return ok;
} bool solve() {
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
sort(a + 1, a + n + 1);
int l = 1, r = n + 1 >> 1;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) l = mid + 1;
else r = mid - 1;
}
cout << r << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

D

题解

知识点:数论,筛法。

注意到,我们要求的是每个元素不超过 \(m\) 的正整数,长度 \([1,n]\) 的每个长度的不明确的序列个数之和。我们先考虑长度为 \(n\) 的情况,其他长度可以同理。

所有序列天生有一组 \([1,1,1,1,\cdots]\) 的删除序列,这代表只要序列有一个元素能在 \(1\) 以外的位置删除,就能产生新的删除序列,则原序列就是不明确的。

因为可以通过移除第一项,让 \(a[i]\) 往前挪,必然会经过 \([2,i]\) 的所有位置,所以若要使 \(a[i]\) 可在 \(1\) 以外的位置删除,需要 \(a[i]\) 存在 \([2,i]\) 内的数与其没有公共质因子,更进一步,即不包含所有前缀素数(\([2,i]\) 所有数的质因子种类,即其中所有素数),这样就一定存在 \(2\leq j\leq i\) 使 \(gcd(j,a[i]) = 1\) 。

注意到,计算在 \(a[i]\) 位置上 \([1,m]\) 中符合条件的数的个数很困难,但计算包含所有前缀质因子的情况很容易, \(\frac{m}{mul_i}\) 就是 \([1,m]\) 所有前缀质因子都存在的数的个数,其中 \(mul_i\) 是位置 \(i\) 的前缀质因子乘积。

我们计算出 \([1,n]\) 每个位置的 \(\frac{m}{mul_i}\) ,即每个位置其前缀质因子都存在数的个数,把他们乘法原理乘在一起,就代表长度为 \(n\) 明确的数列的个数 \(\prod_{i=1}^n \frac{m}{mul_i}\) ,因为每个位置组合的都是包含所有前缀质因子,除了在 \(1\) 处删除,其他地方 \(gcd(i,a[i]) \neq 1\) 不能删。

最后对于长度 \(n\) 的数列,所有情况一共 \(m^n\) 种,所以最后不明确的数列个数为 \(m^n - \prod_{i=1}^n \frac{m}{mul_i}\) 。

我们对 \([1,n]\) 所有长度的答案求和,有 \(ans = \sum_{i=1}^n (m^i - \prod_{j=1}^i \frac{m}{mul_j})\) ,注意到 \(m^i\) 、 \(mul_i\) 以及 \(\prod_{j=1}^i \frac{m}{mul_j}\) 可以从 \(1\) 递推,过程中加到答案里即可。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; const int mod = 998244353; int cnt;
int vis[300007];
int prime[300007] = { 1 };
void euler_screen(int n) {
for (int i = 2;i <= n;i++) {
if (!vis[i]) prime[++cnt] = i;
for (int j = 1;j <= cnt && i * prime[j] <= n;j++) {
vis[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
ll m;
cin >> n >> m;
euler_screen(n);
int base = 1, mul = 1, ans = 0;
ll fact = 1;
for (int i = 1;i <= n;i++) {
if (!vis[i] && fact <= m) fact *= i;
base = 1LL * m % mod * base % mod;
mul = 1LL * m / fact % mod * mul % mod;
ans = ((ans + base) % mod - mul + mod) % mod;
}
cout << ans << '\n';
return 0;
}

E

题解

知识点:bfs。

思考明白了就是一个很简单的01bfs。

注意到我们需要让从第一行到第 \(n\) 行不存在路径,反过来想就是需要一条从第一列到第 \(m\) 列连续的横向仙人掌路径,才能阻挡所有竖向路径,这个路径要求花费最少,于是问题转化问从第一列出发到第 \(m\) 列的仙人掌最短路,起点是第一列所有点,有仙人掌的格子花费为 \(0\) ,没有的花费是 \(1\) 。

搜索过程中用一个 map 记录前驱坐标即可复原路径。

这道题主要在这个思考和转化的过程。

时间复杂度 \(O(nm)\)

空间复杂度 \(O(nm)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; const int dir[4][2] = { {1,1},{1,-1},{-1,1},{-1,-1} };
const int dir2[4][2] = { {1,0},{0,1},{0,-1},{-1,0} }; bool solve() {
int n, m;
cin >> n >> m;
vector<vector<char>> dt(n + 1, vector<char>(m + 1));
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
cin >> dt[i][j];
auto check = [&](int x, int y) {
bool ok = 1;
for (int i = 0;i < 4;i++) {
int xx = x + dir2[i][0];
int yy = y + dir2[i][1];
if (xx <= 0 || xx > n || yy <= 0 || yy > m) continue;
ok &= dt[xx][yy] != '#';
}
return ok;
};
deque<pair<int, int>> dq;
vector<vector<bool>> vis(n + 1, vector<bool>(m + 1));
map<pair<int, int>, pair<int, int>> pre;
pair<int, int> p = { 0,0 };
for (int i = 1;i <= n;i++) {
if (dt[i][1] == '#') dq.push_front({ i,1 }), vis[i][1] = 1, pre[{i, 1}] = { 0,0 };
else if (check(i, 1)) dq.push_back({ i,1 }), vis[i][1] = 1, pre[{i, 1}] = { 0,0 };
}
while (!dq.empty()) {
auto [x, y] = dq.front();
dq.pop_front();
if (y == m) {
p = { x,y };
break;
}
for (int i = 0;i < 4;i++) {
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if (xx <= 0 || xx > n || yy <= 0 || yy > m || vis[xx][yy]) continue;
if (dt[xx][yy] == '#') dq.push_front({ xx,yy }), vis[xx][yy] = 1, pre[{xx, yy}] = { x,y };
else if (check(xx, yy)) dq.push_back({ xx,yy }), vis[xx][yy] = 1, pre[{xx, yy}] = { x,y }; }
}
auto &[px, py] = p;
if (!px && !py) return false;
cout << "YES" << '\n';
while (px || py) {
dt[px][py] = '#';
p = pre[{px, py}];
}
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cout << dt[i][j];
}
cout << '\n';
}
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << "NO" << '\n';
}
return 0;
}

Educational Codeforces Round 138 (Rated for Div. 2) A-E的更多相关文章

  1. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  3. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  5. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  6. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

随机推荐

  1. 故障案例 | 一次慢SQL优化分析全过程

    欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 客 ...

  2. MybatisPlus——全网配置最全的代码生成器

    MybatisPlus代码生成器 这里讲解的是新版 (mybatis-plus 3.5.1+版本),旧版不兼容 官方文档:https://baomidou.com/(建议多看看官方文档,每种功能里面都 ...

  3. Golang基础教程

    以下使用goland的IDE演示,包含总计的golang基础功能共20个章节 一.go语言结构: 二.go基础语法: 三.变量 四.常量 五.运算符 六.条件语句 七.循环 八.函数 九.变量作用域 ...

  4. 【NOI P模拟赛】仙人掌(圆方树,树形DP)

    题面 n n n 个点, m m m 条边. 1 ≤ n ≤ 1 0 5 , n − 1 ≤ m ≤ 2 × 1 0 5 1\leq n\leq 10^5,n-1\leq m\leq 2\times1 ...

  5. 大家都能看得懂的源码 - 那些关于DOM的常见Hook封装(一)

    本文是深入浅出 ahooks 源码系列文章的第十四篇,该系列已整理成文档-地址.觉得还不错,给个 star 支持一下哈,Thanks. 上一篇我们探讨了 ahooks 对 DOM 类 Hooks 使用 ...

  6. c++的一些笔记

    --const 的一些用法 1,修饰指针 const int *p=....   可以改变指针所指的位置,但不能改变指向位置的值. 2,修饰变量 int const * p=....  可以改变指向位 ...

  7. Android平台RTMP/RTSP播放器开发系列--解码和绘制

    本文主要抛砖引玉,粗略介绍下Android平台RTMP/RTSP播放器中解码和绘制相关的部分(Github). 解码 提到解码,大家都知道软硬解,甚至一些公司觉得硬解码已经足够通用,慢慢抛弃软解了,如 ...

  8. K8S部署超过节点的Pod

    在阿里云上部署了一个K8S集群,一master, 两node: 然后执行 kubectl create -f tomcat.yml yaml如下: apiVersion: apps/v1 kind: ...

  9. 高可用代理服务器实现keepalive+squid

    〇.前言 之前单机部署了squid代理服务器,现在实现一下高可用. 还有自定义squid的error页面 准备:两台centos7(1C2GB) ​ 三个可用IP,一主一备一虚拟IP(VIP) 一.安 ...

  10. Reactor And Gev 详解 通俗易懂

    reactor 详解 在类似网关这种海量连接, 很高的并发的场景, 比如有 10W+ 连接, go 开始变得吃力. 因为频繁的 goroutine 调度和 gc 导致程序性能很差. 这个时候我们可以考 ...