比赛链接

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. Linux查看文件内容与处理文件

    Linux查看文件内容与处理文件 目录 Linux查看文件内容与处理文件 查看文件内容 1.查看文件类型 2.查看整个文件 3.查看部分文件 处理文件 1.创建空文件 2.过滤文件内容 3.统计文件内 ...

  2. [kubernetes]安装dashboard

    前言 kubernetes官方文档中的web UI网页管理工具是kubernetes-dashboard,可提供部署应用.资源对象管理.容器日志查询.系统监控等常用的集群管理功能.为了在页面上显示系统 ...

  3. [转帖]GB18030 编码

    https://www.qqxiuzi.cn/zh/hanzi-gb18030-bianma.php GB18030编码采用单字节.双字节.四字节分段编码方案,具体码位见下文.GB18030向下兼容G ...

  4. [转帖]ls 只显示目录

    https://www.cnblogs.com/lavin/p/5912369.html 只显示目录: ls -d */ 在实际应用中,我们有时需要仅列出目录,下面是 4 种不同的方法. 1. 利用 ...

  5. [转帖]如何部署windows版本的oswatcher

    2017-02-22 没有评论 windows上也有os watcher:OSWFW. 目前支持的windows版本是: Windows XP (x86 & x64)Windows 7 (x8 ...

  6. 关于cockpit的学习

    关于cockpit的学习 背景 使用node-exporter 可以监控很多资源使用情况 但是这个需要搭建一套prometheus和grafana的工具 并且每个机器都需要安装一套node-expor ...

  7. [转帖]比 Python 快 35000 倍!LLVM&Swift 之父宣布全新编程语言 Mojo:编程被颠覆了

    https://www.infoq.cn/article/GFfVLVpkIGOcKYB85Opb "Mojo 可能是近几十年来最大的编程语言进步." 近日,由 LLVM 和 Sw ...

  8. [转帖]自动化回归测试工具 —— AREX 上手实践

    https://my.oschina.net/arextest/blog/8589156   AREX 是一款开源的自动化测试工具平台,基于 Java Agent 技术与比对技术,通过流量录制回放能力 ...

  9. [转帖]Nginx-https证书认证详解

    https://developer.aliyun.com/article/885650?spm=a2c6h.24874632.expert-profile.306.7c46cfe9h5DxWK 简介: ...

  10. [转帖]【JVM】常用虚拟机参数及实例

    常用参数表 参数 描述 -XX:+PrintGC 启动java虚拟机后,只要遇到gc,就打印日志 -XX:+PrintGCDetails gc发生时,打印更详细的日志 -XX:+PrintHeapAt ...