Educational Codeforces Round 136 (Rated for Div. 2) A-E
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\) 赢的局面数。 有递推关系:
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)\) ),接下来有两种走法:继续往右直走,换另一行往右直走。
第一种情况往右直走,此时我们需要手动保证 \((i,1-j)\) 没垃圾,但其影响不了机器人下一步行动,所以可以直接转移:
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (s[j][i + 1] == '1'));
第二种情况换行往右直走,但这种情况建立在 \((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的更多相关文章
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- 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 ...
- 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 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- 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 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- Git Clone一个GitHub仓库时,发生报错
1.问题 1.使用HTTP方式:Git: fatal: unable to access ' https://github. com/Light-City/CPlusPlusThings. git/' ...
- PolarD&N2023秋季个人挑战赛—Misc全解
签个到叭 题目信息 压缩包带密码,放到010查看PK头错误,改回去.. 解压后得到 562+5Yiw5Lmf5LiN6IO96L+Z5LmI566A5Y2V5ZGA77yM5b+r5p2l55yL55 ...
- [转帖]在麒麟linux上安装Postgresql12.5
https://jimolonely.github.io/tech/linux/install-postgresql-kylin/ 本文主要实践在麒麟V10版本上通过源码编译安装PostgreSQL1 ...
- Redis6.x 在Windows上面编译安装的过程
背景说明 在github上面仅能够找到 redis3.2.100的Windows安装文件 比较新的版本比较难以找到, 同事经常出现这个版本的redis卡死的情况, 所以想尝试进行一下升级. 第一部分下 ...
- 我对computed的理解-以及computed的传参
computed 传参 <template> <div> <p>computed传参的写法:{{ who1Params('--我是传参的内容') }}</p& ...
- TypeScript中泛型<T>详细讲解
1.泛型 在定义函数或者接口或者类的时候 不能预先确定要使用的数据类型 而是在使用函数.接口.或者类的时候才能够确定数据类型 这个时候我们就需要使用的是泛型 2.功能描述 我们需要实现一个方法,方法中 ...
- 【K哥爬虫普法】微信公众号爬虫构成不正当竞争,爬虫er面对金山,如何避免滥用爬虫?
我国目前并未出台专门针对网络爬虫技术的法律规范,但在司法实践中,相关判决已屡见不鲜,K 哥特设了"K哥爬虫普法"专栏,本栏目通过对真实案例的分析,旨在提高广大爬虫工程师的法律意识, ...
- docker 发布web项目到linux
一.准备工作 1.连接到Liunx的工具 MobaXterm 填好Ip 直接点ok就行 输入用户名和密码进入系统 2.已发布的.netcore网站或微服务 在要发布的项目上右键---->添加-- ...
- Vue双向数据绑定原理-上
Vue响应式的原理(数据改变界面就会改变)是什么? 时时监听数据变化, 一旦数据发生变化就更新界面, 这就是Vue响应式的原理. Vue是如何实现时时监听数据变化的 通过原生JS的defineProp ...
- Python自动化办公--Pandas玩转Excel数据分析【二】
相关文章: Python自动化办公--Pandas玩转Excel[一] Python自动化办公--Pandas玩转Excel数据分析[三] python处理Excel实现自动化办公教学(含实战)[一] ...