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 ...
随机推荐
- Synchronized的使用及原理总结
本文为博主原创,未经允许不得转载 Synchronized的使用总结: 1.作用 原理 synchronized 的锁膨胀升级过程 对象的内存布局 锁的消除及逃逸分析 synchronized的方 ...
- 8. exporter
一.已经实现的收集器 1.1 可收集的内存指标 1.2 可收集的jetty指标 二.自定义收集 2.1 summer 2.2 histogram 三.架构设计 exporter作为Prometheus ...
- Go-稀疏数组
package main import "fmt" // 稀疏数组 // 1. 二维数组 // 2. 存在大量相同相同数据和少量不同数据 // 思维: 将大量相同数据转化为: (数 ...
- 【转帖】linux 调优篇 :硬件调优(BIOS配置)* 壹
一. 设置内存刷新频率为Auto二. 开启NUMA三. 设置Stream Write Mode四. 开启CPU预取配置五. 开启SRIOV六. 开启SMMU 通过在BIOS中设置一些高级选项,可以有效 ...
- [转帖]apt update和apt upgrade命令 - 有什么区别?
在之前的文章中,我们查看了APT 命令以及您可以使用包管理器来管理包的各种方法.这是一个总体概述,但在本指南中,我们暂停并重点关注 2 个命令用法.这些是apt update和apt upgrade命 ...
- [转帖]ubuntu下配置iptables、ufw端口转发
iptables 端口转发(CentOS) 注意:一来一去 在中转服务器操作 iptables -t nat -A PREROUTING -p tcp --dport [端口号] -j DNAT -- ...
- [转帖]什么是拒绝服务(DoS)攻击?
https://www.cloudflare.com/zh-cn/learning/ddos/glossary/denial-of-service/ 什么是拒绝服务攻击? 拒绝服务(DoS)攻击是一种 ...
- 基于华为fusionstorage的块存储CSI
承接上文,块存储的CSI要比对象存储复杂一些,但总的处理逻辑还是一致的.下面以华为fusionstorage的CSI为例进行介绍,该插件支持了多个后端存储,如fusionstorage和oceanst ...
- FinClip 小程序的自有账户体系是怎么做的?
随着公司规模越来越大,员工需要使用的产品矩阵也会越来越丰富,不仅包括内部的 IT 系统,OA 系统,业务系统,还会有很多和外部产品集成的登录流程,更别提各种业务系统或者子系统中的账户体系了.如果使用简 ...
- 解锁前端新潜能:如何使用 Rust 锈化前端工具链
前言 近年来,Rust的受欢迎程度不断上升.首先,在操作系统领域,Rust 已成为 Linux 内核官方认可的开发语言之一,Windows 也宣布将使用 Rust 来重写内核,并重写部分驱动程序. ...