比赛链接

A

题解

知识点:模拟。

所有点都跑一遍即可。

另外可以不模拟, \(\geq 2*2\) 的情况都可以摆在 \((2,2)\) 这个点,其他摆在 \((1,1)\) 。

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

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

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; const int dir[8][2] = { {-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1} };
bool solve() {
int n, m;
cin >> n >> m;
int x = n, y = m;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
bool ok = false;
for (int k = 0;k < 8;k++) {
int xx = i + dir[k][0];
int yy = j + dir[k][1];
ok |= xx >= 1 && xx <= n && yy >= 1 && yy <= m;
}
if (!ok) {
x = i;
y = j;
break;
}
}
}
cout << x << ' ' << y << '\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;
}

B

题解

知识点:贪心。

显然,根据 \(d_i\) 可以得到 \(a_i = a_{i-1} \pm d_i\) ,但 \(a_i \geq 0\) ,因此如果 \(d_i \neq 0\) 且 \(a_{i-1} - d_i \geq 0\) 时说明有两个合法 \(a_i\) 输出 \(-1\) ,否则继续构造。

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

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

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int d[107];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> d[i];
for (int i = 2;i <= n;i++) {
if (d[i] && d[i - 1] - d[i] >= 0) return false;
d[i] += d[i - 1];
}
for (int i = 1;i <= n;i++) cout << d[i] << ' ';
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 << -1 << '\n';
}
return 0;
}

C

题解

知识点:排列组合,构造,线性dp。

总局面一共 \(C_{n}^{\frac{n}{2}}\) 种。

注意到平局只有 \(1\) 种情况,即每次最大的牌都在对方手里,而自己手里有次大的牌,如此就能构造出平局的局面了。比如在 \(n = 8\) 时,平局为 \(A\) 拥有 \(7632\) ,\(B\) 有 \(8541\) 。因此只要求出 \(A\) 赢局面,就能得到 \(B\) 赢局面。

如果 \(A\) 的局面比其平局局面的从大到小的某一回合牌更大(前面回合的牌和平局一样,后面的牌随意分配),那么 \(A\) 赢。如 \(A\) 有 \(7641\) ,\(76\) 一样但 \(4\) 比 \(3\) 大,因此 \(A\) 赢。对 \(B\) 也是同理的。

再者,我们注意到,局面情况是可以递推的。如 \(n=8\) 时第一回合结束就来到 \(n=6\) 的情况,不过 \(B\) 成先手了。

因此我们对 \(n = i\) 的局面,可以先求出第一回合的情况,再用 \(n=i-2\) 的结果即可。

显然第一回合 \(A\) 赢的情况,只有牌是最大 \(i\) 的时候,此时剩下的牌可以随便选,一共有 \(C_{i-1}^{\frac{i}{2}-1}\) 种,接下来的情况和 \(n=i-2\) 时 \(B\) 赢等价。

我们定义 \(ansA[i]\) 为 \(n = i\) 时 \(A\) 赢的局面数,\(ansB[i]\) 为 \(n=i\) 时 \(B\) 赢的局面数。 有递推关系:

\[\begin{aligned}
ansA[i] &= C_{i-1}^{\frac{i}{2}-1} + ansB[i-2]\\
ansB[i] &= C_{i}^{\frac{i}{2}} - ansA[i]-1
\end{aligned}
\]

时间复杂度 \(O(60^2)\)

空间复杂度 \(O(60^2)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; const int mod = 998244353; int ansA[67], ansB[67];
int C[67][67]; void init(int n = 60) {
for (int i = 0;i <= n;i++) {
for (int j = 0;j <= i;j++) {
if (j == 0 || i == j) C[i][j] = 1;
else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
}
} void get(int n = 60) {
ansA[2] = 1;
ansB[2] = 0;
for (int i = 4;i <= n;i += 2) {
ansA[i] = (C[i - 1][i / 2 - 1] + ansB[i - 2]) % mod;
ansB[i] = (C[i][i / 2] - ansA[i] - 1 + mod) % mod;
}
} bool solve() {
int n;
cin >> n;
cout << ansA[n] << ' ' << ansB[n] << ' ' << 1 << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
init();
get();
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

D

题解

知识点:二分,树形dp。

这道题答案具有单调性,因此可以二分答案 \(mid\) 。

设 \(dp[u]\) 为节点 \(u\) 希望的高度。自底向上处理,把叶子节点希望高度设为 \(mid\) 。父节点的希望高度为所有子节点希望高度最小值减 \(1\) 。

当 \(dp[u] = 1\) 时,除了节点 \(1\) 和父节点是 \(1\) 的节点,都属于需要操作的节点,因此操作次数加 \(1\) 。但要注意,操作过的节点希望高度需要设为 \(mid+1\) ,这个子节点已经与原来的父节点分离了,其希望高度不能影响到原来的父节点,所以设为父节点可能的希望高度最大值加 \(1\) 。

最后判断一下操作次数有没有超过 \(k\) 来进行二分即可。

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

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

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; vector<int> g[200007];
int dp[200007]; int n, k;
int mid, cnt;
void dfs(int u, int fa) {
dp[u] = mid;
for (auto v : g[u]) {
if (v == fa) continue;
dfs(v, u);
dp[u] = min(dp[u], dp[v] - 1);
}
if (dp[u] == 1 && fa != 1 && u != 1) {
cnt++;
dp[u] = mid + 1;
}
} bool check(int mid) {
cnt = 0;
dfs(1, 0);
return cnt <= k;
} bool solve() {
cin >> n >> k;
for (int i = 1;i <= n;i++) g[i].clear();
for (int i = 2;i <= n;i++) {
int u;
cin >> u;
g[i].push_back(u);
g[u].push_back(i);
} int l = 1, r = n - 1;
while (l <= r) {
mid = l + r >> 1;
if (check(mid)) r = mid - 1;
else l = mid + 1;
}
cout << l << '\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;
}

E

题解

知识点:状压dp。

设 \(dp[i][j]\) 表示清理到第 \(i\) 列第 \(j\) 行格子且第 \(i-1\) 列及以前的格子也都清理时,机器人清理的最大格子数。

假设机器人走到了第 \(i\) 列第 \(j\) 行(接下来称为 \((i,j)\) ),接下来有两种走法:继续往右直走,换另一行往右直走。

  1. 第一种情况往右直走,此时我们需要手动保证 \((i,1-j)\) 没垃圾,但其影响不了机器人下一步行动,所以可以直接转移:

    dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (s[j][i + 1] == '1'));
  2. 第二种情况换行往右直走,但这种情况建立在 \((i,1-j)\) 有垃圾,否则机器人不会换行。此时我们需要手动保证 \((i+1,j)\) 没垃圾,但不能直接转移到 \((i+1,1-j)\) ,因为此时转移的状态是建立在 \((i+1,j)\) 一定没有垃圾的情况。但 \((i+1,1-j)\) 也有可能会从 \((i+1,j)\) 转移过来,这个转移是没有限制的。因此转移的前提都不同了,所以第二种转移具有后效性。解决方案是继续顺着走到 \((i+2,1-j)\),既然 \((i+1,j)\) 在这种情况下保证没有垃圾了,不妨继续走到 \((i+2,1-j)\) ,此时是没有后效性的,转移方程为:

    dp[min(i + 2, n)][j ^ 1] = max(dp[min(i + 2, n)][j ^ 1], dp[i][j] + 1 + (s[j ^ 1][i + 1] == '1') + (s[j ^ 1][i + 2] == '1'));

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

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

代码

#include <bits/stdc++.h>

using namespace std;

int dp[200007][2];
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
string s[2];
cin >> s[0] >> s[1];
memset(dp, -0x3f, sizeof(dp));
dp[0][0] = 0;
for (int i = 0;i < n;i++) {
for (int j = 0;j < 2;j++) {
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (s[j][i + 1] == '1'));
if (s[j ^ 1][i] == '1')
dp[min(i + 2, n)][j ^ 1] = max(dp[min(i + 2, n)][j ^ 1], dp[i][j] + 1 + (s[j ^ 1][i + 1] == '1') + (s[j ^ 1][i + 2] == '1'));
}
}
cout << max(dp[n][0], dp[n][1]) << '\n';
return 0;
}

Educational Codeforces Round 136 (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 ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. RL 的探索策略 | Exploration for RL

    最近在草率地调研 RL 的 exploration. 这篇文章也比较草率,仅能起到辅助作用,不能代替读 review 或更精细的读 paper. 目录 0 总结写在最前面 1 主要参考资料 2 RL ...

  2. C# WPF侧边栏导航菜单(Dropdown Menu)

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  3. 在虚拟机(Linux)中Docker中部署Nginx成功,但是在宿主机无法访问Nginx站点?

    1.问题 本文是基于黑马程序员Docker基础--常见命令一课中部署Nginx时遇到的问题作出解答. 在虚拟机(Linux)中Docker中部署Nginx成功,但是在宿主机无法访问Nginx站点 如图 ...

  4. js - body的滚动事件的坑

    文章来源 : https://www.cnblogs.com/Zting00/p/7497629.html 踩过些坑,得到的结论,不一定精确 1. body的滚动条,刷新页面的时候不会回到顶部.其他d ...

  5. 阿里云龙蜥8.6部署SQLSERVER2022的过程

    阿里云龙蜥8.6部署SQLSERVER2022的过程 背景 之前总结过, 但是发现当时是preview版本. 这里想升级一下, 并且顺便抄一下他的部分说明 下载 wget https://packag ...

  6. [转帖]INTEL MLC(Memory Latency Checker)介绍

    https://zhuanlan.zhihu.com/p/359823092 在定位机器性能问题的时候,有时会觉得机器莫名其妙地跑的慢,怎么也看不出来问题.CPU频率也正常,程序热点也没问题,可就是慢 ...

  7. 一个Redis dump文件的简要分析过程

    摘要 遇到一个老大难的问题. 让帮忙分析一下一个Redis的dump文件. 虽然之前写过了rdb和rdr的文档 但是感觉大家都喜欢拿来主义. 没办法. 今天继续进行深入一点的分析. 原理其实还是基于r ...

  8. [粘贴]【CPU】关于x86、x86_64/x64、amd64和arm64/aarch64

    [CPU]关于x86.x86_64/x64.amd64和arm64/aarch64 https://www.jianshu.com/p/2753c45af9bf 为什么叫x86和x86_64和AMD6 ...

  9. 对于Vue3和Ts的心得和思考

    作者:京东物流 吴云阔 1 前言 Vue3已经正式发布了一段时间了,各种生态已经成熟.最近使用taro+vue3重构冷链的小程序,经过了一段时间的开发和使用,有了一些自己的思考. 总的来说,Vue3无 ...

  10. 感性理解 int 与 long long 的速度差距 & 感性理解不同取模方法的差距

    long long 题该怎么做?#define int long long 会多慢? 有时候,当我们被卡常的时候,不妨想一想,自己在开头定义的 #define int long long 有多大影响? ...