Educational Codeforces Round 138 (A-E)题解
A Cowardly Rooks
思路:看着还挺复杂的。仔细观察一下题意,题中已经说明初始情况没有两个棋子在同一行或同一列,那么只要有空缺行/空缺列,就可以移动。只要判断n等不等于m就行。
代码:
#include <bits/stdc++.h>
using namespace std;void solve() {int n, m, x, y;cin >> n >> m;for (int i = 0; i < m; i++) {cin >> x >> y;}if (n > m)cout << "YES" << endl;elsecout << "NO" << endl;return;
}int main() {int t;cin >> t;while (t--) {solve();}return 0;
}
B Death's Blessing
思路:我们发现,为了杀死所有怪物,必须要消耗掉数组a[N]之和的时间以及怪物的亡语额外赋予的时间。问题转化为如何让怪物的亡语赋予的时间最小。由于只要场上的怪物>=2,那么亡语至少会对一个怪物生效。因此赋予的时间不能低于b[N]之和减去b[N]的最小值。接下来我们证明这种情况一定可以成立:只要从两边分别杀死怪物,最终留下b最小的那个怪物即可。因此可以证明答案为sum(a[])+sum(b[])-min(b[])。
代码:
#include <bits/stdc++.h>
using namespace std;void solve() {int n;cin >> n;long long sum = 0, tp, maxm = 0;for (int i = 0; i < n; i++) {cin >> tp;sum += tp;}for (int i = 0; i < n; i++) {cin >> tp;sum += tp;if (maxm < tp)maxm = tp;}cout << sum - maxm << endl;return;
}int main() {int t;cin >> t;while (t--) {solve();}return 0;
}
C Number Game
思路:这题可以先进行一些尝试。
如果能进行1个round,那么一定有一个1,否则,Alice就输了。
如果能进行2个round,那么round1需要1个2(或比2更小的数),而round2需要1个1,但Bob会将尽可能小的数变大以防止Alice赢,故实际上需要的是2个1和1个2(或更小的数)。
同理,如果能进行3个round,那么Bob可以把数变大两次,因此必须要有1 1 1 2 3(或更小的数),
4个round,就是1 1 1 1 2 3 4(或更小)……
因此排序后O(n^2)遍历解决。
代码:
#include <bits/stdc++.h>
using namespace std;void solve() {int n, a[200];cin >> n;for (int i = 0; i < n; i++) {cin >> a[i];}sort(a, a + n);for (int i = 0; i < (n + 1) / 2; i++) {for (int j = i; j <= 2 * i; j++) {if (a[j] > j - i + 1) {cout << i << endl;return;}}}cout << (n + 1) / 2 << endl;return;
}int main() {int t;cin >> t;while (t--) {solve();}return 0;
}
D Number Game
思路:观察样例,为什么4 2输出的是26?
1位时不行。2位时,只要第2位不能整除2就行。3位和4位所有情况都可以。答案是2+8+16。
再推演一下样例4 6是如何导出结果的。1位、2位时同上。3位时,如果第2位整除2,且第3位整除6,就不符合题意了。那么第4位呢?我们发现,比如4个数是1 2 6 6时就会出问题。因为gcd(6,4)、gcd(6,3)、gcd(6,2)都不为1。推演一下后发现最终答案是6*6*6*6-1*1*3*6+6*6*6-1*3*6+6*6-3*6=1494。
概括总结出规律,第2位开始整除2会出问题,第3位开始同时整除2和3会出问题,第5位开始同时整除2和3和5会出问题……如果所有的位都出问题,那么这种情况就不符合题意。也就是说,每一次遇到素数时,不能同时整除的数就多了一个。因此枚举位数,用乘法原理,把不符合题意的扣除即可。
代码:
#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;void solve() {long long nn, mm;long long sum = 0, n, m, jz = 2, bt = 1;cin >> nn >> mm;n = nn;m = mm;bt = m % mod;for (long long i = 2; i <= nn; i++) {if (i == 3 || i == 5 || i == 7 || i == 11 || i == 13 || i == 17 || i == 19 || i == 23 || i == 29 || i == 31)jz *= i;m = ((mm % mod) * (m % mod)) % mod;sum = (sum + m) % mod;if (i < 37) {bt = ((bt % mod) * ((mm / jz) % mod)) % mod;sum = (sum + mod - bt) % mod;}}cout << sum << endl;return;
}int main() {int t;solve();return 0;
}
E Cactus Wall
思路:这是一个0-1bfs问题,如果正好走在仙人掌上,就记0,不走在仙人掌上,就记1,要求最后数值尽可能小。使用双端队列,如果是0就加入双端队列的前面,是1就加入双端队列的后面,这样就避免了排序和重复搜索的问题,可以让bfs在O(点的个数)时间内完成。
核心是以下部分组成:1.定义函数判断是否合法,即点在范围内且周围没有仙人掌。2.初始情况入队,即x=0时的每一个点都要作为初始情况入队。3.具体的0-1bfs过程,求出最小值路径。4.回溯最后走出的最小值路径,把‘.’改成‘#’。最后输出。
#include <bits/stdc++.h>
using namespace std;
int n, m;int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, -1, 1, -1, 1};
bool rg(int x, int y) {return 0 <= x && x < n && 0 <= y && y < m;
}
bool legal(int x, int y, char *s) {if (!rg(x, y))return false;for (int i = 0; i < 4; i++) {int newx = x + dx[i], newy = y + dy[i];if (rg(newx, newy) && s[newx * (m + 1) + newy] == '#') {// cout << newx << "sb " << newy << endl;return false;}}//cout << x << "cg " << y << endl;return true;
}void solve() {cin >> n >> m;char s[n][m + 1];for (int i = 0; i < n; i++) {scanf("%s", s[i]);}deque<pair<int, int>> q;int d[n][m], p[n][m];memset(d, -1, sizeof(d));memset(p, 0, sizeof(p));for (int i = 0; i < n; i++) {if (s[i][0] == '#') {d[i][0] = 0;q.push_front({i, 0});} else if (legal(i, 0, *s)) {d[i][0] = 1;q.push_back({i, 0});}}while (!q.empty()) {int x, y, w;x = q.front().first;y = q.front().second;// cout << x << y << endl;q.pop_front();for (int i = 4; i < 8; i++) {int newx = x + dx[i], newy = y + dy[i];if (!legal(newx, newy, *s))continue;if (s[newx][newy] == '#')w = 0;elsew = 1;if (d[newx][newy] == -1 || d[newx][newy] > d[x][y] + w) {d[newx][newy] = d[x][y] + w;p[newx][newy] = i;if (w == 1)q.push_back({newx, newy});elseq.push_front({newx, newy});}}}int x = 0;for (int i = 0; i < n; i++) {if (d[x][m - 1] == -1 || (d[i][m - 1] != -1 && d[x][m - 1] > d[i][m - 1])) {x = i;}}if (d[x][m - 1] == -1) {cout << "NO" << endl;return;}int y = m - 1;while (1) {s[x][y] = '#';int i = p[x][y];if (!i)break;x -= dx[i];y -= dy[i];}cout << "YES" << endl;for (int i = 0; i < n; i++) {printf("%s\n", s[i]);}return;
}int main() {int t;cin >> t;while (t--) {solve();}return 0;
}
Educational Codeforces Round 138 (A-E)题解相关推荐
- 【Educational Codeforces Round 138】A. Cowardly Rooks
Educational Codeforces Round 138中A. Cowardly Rooks Codeforces比赛记录 文章目录 题目链接: 一.A. Cowardly Rooks 题目意 ...
- Educational Codeforces Round 138 (Rated for Div. 2)-赛后总结
Dashboard - Educational Codeforces Round 138 (Rated for Div. 2) - Codeforces 总结一个教训就是不要心急,特别是对于一些题目, ...
- Educational Codeforces Round 138 (Rated for Div. 2) D
Educational Codeforces Round 138 (Rated for Div. 2) D. Counting Arrays 题意 给定长度为nnn的数组aaa,若gcd(ai,i)= ...
- Educational Codeforces Round 138 (Rated for Div. 2) A~D
比赛链接:Dashboard - Educational Codeforces Round 138 (Rated for Div. 2) - Codeforces 目录 A. Cowardly Roo ...
- Educational Codeforces Round 138 (Rated for Div. 2) C. Number Game 解题报告
原题链接: Problem - C - Codeforces 题目描述: Alice and Bob are playing a game. They have an array of positiv ...
- Educational Codeforces Round 138 (Rated for Div. 2)
文章目录 一.A. Cowardly Rooks 二.B - Death's Blessing 三.C - Number Game 四.D - Counting Arrays 四.E - Cactus ...
- Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...
- Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...
- Educational Codeforces Round 140 (Rated for Div. 2)题解
看看时间还有十几分钟,开不出来题了,写个题解 A. Cut the Triangle 检查是不是直角边平行于坐标轴的直角三角形即可 这里可以用异或来写,代码较为简洁,我就不改了,直接贴上我的丑代码 c ...
最新文章
- 基于 Kafka 与 Debezium 构建实时数据同步
- 压缩比13为什么建议用92的油_92号和95号汽油,哪个更耐烧?车主:欢迎入坑
- OLTP 系统和 OLAP 系统的核心设计思想
- matlab fname pname,求大神帮我解释一下matlab最后几行是什么意思
- github ssh 配置_怎么给Git配置多个SSH Key?
- Spark in action on Kubernetes - 存储篇(一)
- Codeforces Round #499 (Div. 2): F. Mars rover(DFS)
- J2EE Architecture(6)
- Linux-用户操作
- 利用python进行TEQC质量检核结果绘图
- Table is marked as crashed and should be repaire (
- 分享下nirsoft提供的注册表工具
- zencart 模板文件说明
- 线段树入门之夜空星亮
- Unity_7 如何使用遮挡剔除Occlusion Culling
- 企业面试遇到的问题02
- WebStorm/IDEA 激活证书服务器
- tar 慢 加快_加快慢的Outlook 2007
- 五种知名的分布式数据库大PK
- rvm安装和安装失败问题解决,使用rvm安装ruby