Educational Codeforces Round 118 比赛记录
liyuanchen2021 · · 个人记录
Educational Codeforces Round 118 比赛记录
比赛链接
比赛成绩
| Solved | Penalty | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|
| upsolved | 超纲 |
注:“超纲”代表超出 NOI 大纲中提高级内容,暂不作具体讲述。
比赛过程
重现赛时间:2024.7.17 14:30~16:30
前三题做题状态还不错,都提交一遍就过了。
D 题是道 DP 题,但没有什么状态设计的思路,所以直接跳过 D 题开了 E 题。
在 E 题的样例中,我看到了一些字符矩阵。原来,这道题涉及到了搜索算法。
我又想了几分钟 D 题,只能想到前缀和的做法了,但是这一做法会超时,就没有再往下做了。
题解
A. Long Comparison
思路
根据题意,第一个数
我们先把
下面,我们进行分类讨论:
如果
x_1 > x_2 :若
p_1 > 0 ,则a > b 。否则,将
x_2 乘上{10}^{p_2} ,并判断此时x_1 和x_2 的大小关系。如果
x_1 < x_2 :若
p_2 > 0 ,则a < b 。否则,将
x_1 乘上{10}^{p_1} ,并判断此时x_1 和x_2 的大小关系。若
x_1=x_2 ,则直接判断p_1 和p_2 的大小关系即可。
代码
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int T; cin >> T;
while (T--) {
int x1, p1, x2, p2;
cin >> x1 >> p1 >> x2 >> p2;
int m = min(p1, p2);
p1 -= m, p2 -= m;
if (x1 > x2) {
if (p1) puts(">");
else {
while (x1 >= x2 && p2) x2 *= 10, p2--;
if (x1 > x2) puts(">");
else if (x1 == x2) puts("=");
else puts("<");
}
} else if (x1 < x2) {
if (p2) puts("<");
else {
while (x1 <= x2 && p1) x1 *= 10, p1--;
if (x1 > x2) puts(">");
else if (x1 == x2) puts("=");
else puts("<");
}
} else {
if (p1 > p2) puts(">");
else if (p1 == p2) puts("=");
else puts("<");
}
}
return 0;
}
B. Absent Remainder
思路
我们之前学到过这样一个定理:
一个数除以另一个数的余数总是小于除数。
由于
代码
#include <iostream>
#include <algorithm>
using namespace std;
int n, a[200005];
int main() {
int T; cin >> T;
while (T--) {
cin >> n;
int m = n / 2;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
n = unique(a + 1, a + n + 1) - a - 1;
for (int i = 2; i <= m + 1; i++) cout << a[i] << ' ' << a[1] << '\n';
}
return 0;
}
C. Poisoned Dagger
思路
由于答案满足二分性,我们可以用二分答案来通过本题。
我们可以通过二分枚举
代码
#include <iostream>
#define int long long
using namespace std;
int n, h, a[105];
bool check(int k) {
int pos = 0, hp = 0;
for (int i = 1; i <= n && hp < h; i++) {
hp += a[i] + k - 1 - max(a[i], pos) + 1;
pos = a[i] + k;
}
return hp >= h;
}
signed main() {
int T; cin >> T;
while (T--) {
cin >> n >> h;
for (int i = 1; i <= n; i++) cin >> a[i];
int l = 1, r = 1ll * 1e18, ans = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
cout << ans << '\n';
}
return 0;
}
D. MEX Sequences
思路
这道题可以用 dp 来做。
我们用
初始化
首先,我们考虑子序列中没有比
- 在以
a_i 为结尾的子序列后面添加a_i ,dp_{i,a_i+1,0} = dp_{i,a_i+1,0} + dp_{i-1,a_i+1,0} - 在以
a_i-1 为结尾的子序列后面添加a_i ,dp_{i,a_i+1,0} = dp_{i,a_i+1,0} + dp_{i-1,a_i,0}
其次,我们考虑子序列中含有比
-
dp_{i,a_i+1,1} = dp_{i,a_i+1,1} + dp_{i-1,a_i+1,1} - 当
a_i>0 时,dp_{i,a_i-1,1} = dp_{i,a_i-1,1} + (dp_{i-1,a_i-1,0}+ dp_{i-1,a_i-1,1})
最后,我们把所有
以上做法的的时间复杂度为
注意取模。尤其是在最后输出 Wrong answer on test 29。
代码
#include <iostream>
#define int long long
#define MOD 998244353
using namespace std;
int n, a[500005], dp[500005][2];
signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
int T; cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i <= n + 1; i++) dp[i][0] = dp[i][1] = 0;
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
dp[a[i] + 1][0] = (dp[a[i] + 1][0] + dp[a[i] + 1][0]) % MOD;
dp[a[i] + 1][0] = (dp[a[i] + 1][0] + dp[a[i]][0]) % MOD;
dp[a[i] + 1][1] = (dp[a[i] + 1][1] + dp[a[i] + 1][1]) % MOD;
if (a[i] > 0) {
dp[a[i] - 1][1] = (dp[a[i] - 1][1] + dp[a[i] - 1][1]) % MOD;
dp[a[i] - 1][1] = (dp[a[i] - 1][1] + dp[a[i] - 1][0]) % MOD;
}
}
int ans = 0;
for (int i = 0; i <= n + 1; i++) ans = (ans + (dp[i][0] + dp[i][1]) % MOD) % MOD;
cout << (ans - 1 + MOD) % MOD << '\n';
}
return 0;
}
E. Crazy Robot
思路
我们可以进行深度优先搜索或广度优先搜索,这里我写的是深搜代码。
我们从实验室 L 所在的位置开始搜索。每次搜索时,若该方格为 .,则统计该方格上下左右字符 . 的数量 +。接下来,若此时该方格为 + 或 L,则对周围有 . 的方格进行搜索。
为了保证代码的可读性,作者已对赛时提交的代码稍作修改,修改后的代码见下:
代码
#include <iostream>
#include <vector>
using namespace std;
vector<string> a;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int n, m;
void dfs(int x, int y) {
if (a[x][y] == '.') {
int cnt = 0;
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx < 0 || yy < 0 || xx >= n || yy >= m) continue;
if (a[xx][yy] == '.') cnt++;
}
if (cnt <= 1) a[x][y] = '+';
}
if (a[x][y] == '+' || a[x][y] == 'L') {
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx < 0 || yy < 0 || xx >= n || yy >= m) continue;
if (a[xx][yy] == '.') dfs(xx, yy);
}
}
}
int main() {
int T; cin >> T;
while (T--) {
cin >> n >> m;
a.clear(); a.resize(n);
for (int i = 0; i < n; i++) cin >> a[i];
int lx, ly;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 'L') {
lx = i, ly = j;
break;
}
}
}
dfs(lx, ly);
for (int i = 0; i < n; i++) cout << a[i] << '\n';
}
return 0;
}
F. Tree Coloring
备注
涉及快速傅里叶变换 (FFT) ,超出 NOI 大纲中提高级内容,暂不作具体讲述。