比赛链接

A

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
cout << n - x + 1 << " \n"[i == 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

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; int lena[400007], lenb[400007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= 2 * n;i++) lena[i] = lenb[i] = 0;
int pre = 0, len = 0;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
if (x != pre) {
lena[pre] = max(lena[pre], len);
len = 0;
pre = x;
}
len++;
}
lena[pre] = max(lena[pre], len);
len = 0;
pre = 0;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
if (x != pre) {
lenb[pre] = max(lenb[pre], len);
len = 0;
pre = x;
}
len++;
}
lenb[pre] = max(lenb[pre], len);
int ans = 0;
for (int i = 1;i <= 2 * n;i++) ans = max(ans, lena[i] + lenb[i]);
cout << ans << '\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

题意

一棵树有 \(n\) 个节点,一开始只有根节点 \(1\) 。

现在给出加边的顺序,每次操作按顺序从头到尾依次加边。

一次操作中,当且仅当一条边还没被加,且一个端点已经存在,才能加这条边。

问要最少操作几次,所有边才能都被加入。

题解

知识点:树形dp,DFS。

可以用树型dp求出每个点出现需要的最少操作次数。

点的出现时间取决于边的顺序,我们将边的顺序记录到边权中,dfs过程中将边权当作孩子的出现时间。

若一个点出现时间比它孩子出现时间要晚,那么它孩子出现需要的操作次数就是它的操作次数加 \(1\) ,否则可以在同一次操作中处理完。

设 \(f_u\) 表示节点 \(u\) 出现需要的最少操作次数, \(rk_u\) 表示节点 \(u\) 的出现时间,转移即上述所说。

最后答案为 \(f_u\) 的最大值。

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

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

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; vector<pair<int, int>> g[200007]; int rk[200007], f[200007];
void dfs(int u, int fa) {
for (auto [v, w] : g[u]) {
if (v == fa) continue;
rk[v] = w;
f[v] = f[u] + (w < rk[u]);
dfs(v, u);
}
} bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) g[i].clear();
for (int i = 1;i <= n - 1;i++) {
int u, v;
cin >> u >> v;
g[u].push_back({ v,i });
g[v].push_back({ u,i });
} rk[1] = n;
dfs(1, 0); cout << *max_element(f + 1, f + n + 1) << '\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;
}

D

题意

给定长度为 \(n\) 的数组 \(a,b\) ,求出所有满足 \(a_i \cdot a_j = b_i + b_j (1 \leq i<j \leq n)\) 的 \((i,j)\) 对数。

题解

知识点:数学,枚举。

注意到 \(1 \leq a_i,b_i \leq n (1\leq i \leq n)\) ,因此 \(a_i \cdot a_j \leq 2n\) ,即 \(\min(a_i,a_j) \leq \sqrt{2n}\) 。此时,我们设 \(a_j \leq a_i\) ,那么 \(a_j \leq \sqrt{2n}\) 。

因此,我们先将 \(a_i,b_i\) 打包,以 \(a\) 为关键字从小到大排序,这是为了之后从左到右枚举 \(a_i,b_i\) 时,保证满足 \(a_j \leq a_i(1 \leq j < i)\) 这个条件,此时 \(a_j\) 的范围才是 \([1,\sqrt{2n}]\) 。

随后,我们枚举 \(A \in [1,\sqrt{2n}]\) ,作为一轮枚举中 \(a_j\) 满足的值。接下来,从左到右枚举 \(a_i,b_i\) ,有 \(B = A \cdot a_i - b_i\) ,其中 \(B\) 就是在 \(a_j = A\) 时我们希望的 \(b_j\) ,而 \(a_i,b_i\) 的贡献即为 \(1 \leq j <i\) 满足 \(a_j = A\) 的位置中,出现过的 \(b_j = B\) 的位置数。

因此,我们在过程中记录 \(1 \leq j <i\) 满足 \(a_j = A\) 的位置中,出现过的 \(b_j\) 的个数 \(cnt_{b_j}\)。那么,当 \(1 \leq B \leq n\) 时, \(cnt_B\) 即为 \(a_i,b_i\) 的贡献。

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

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

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; pair<int, int> c[200007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++)cin >> c[i].first;
for (int i = 1;i <= n;i++)cin >> c[i].second;
sort(c + 1, c + n + 1); ll ans = 0;
for (int A = 1;A * A <= 2 * n;A++) {
vector<int> cnt(n + 1);
for (int i = 1;i <= n;i++) {
auto [a, b] = c[i];
int B = A * a - b;
if (1 <= B && B <= n) ans += cnt[B];
if (a == A) cnt[b]++;
}
}
cout << ans << '\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;
}

Codeforces Round #875 (Div. 2) A-D的更多相关文章

  1. Codeforces Round #441 (Div. 2)【A、B、C、D】

    Codeforces Round #441 (Div. 2) codeforces 876 A. Trip For Meal(水题) 题意:R.O.E三点互连,给出任意两点间距离,你在R点,每次只能去 ...

  2. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  3. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  4. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  7. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  8. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  10. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

随机推荐

  1. 新一代自动化测试神器Playwright

    转载请注明出处️ 作者:测试蔡坨坨 原文链接:caituotuo.top/4bedb73c.html 你好,我是测试蔡坨坨. 说到WebUI自动化测试,首当其冲的当属Selenium,在很长的一段时间 ...

  2. GPT-4:思考的曙光还是数据的缩影?

    海盗分金,GPT-4初露锋芒 GPT系列模型横空出世后,其是否真实具有思考和推理的能力一直被业界关注.GPT-3.5在多条狗问题和海盗分金问题上表现糟糕.GPT-4在这两个谜题上给出的答案令人惊喜,甚 ...

  3. Redis性能瓶颈揭秘:如何优化大key问题?

    1. 什么是Redis大key问题 Redis大key问题指的是某个key对应的value值所占的内存空间比较大,导致Redis的性能下降.内存不足.数据不均衡以及主从同步延迟等问题. 到底多大的数据 ...

  4. 没有杯子的世界:OOP设计思想的应用实践

    最近看到一个有趣的问题:Person类具有Hand,Hand可以操作杯子Cup,但是在石器时代是没有杯子的,这个问题用编程怎么解决? 简单代码实现 我们先用简单代码实现原问题: @Data publi ...

  5. 开心档之MySQL ALTER命令

    MySQL ALTER命令 当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 开始本章教程前让我们先创建一张表,表名为:testalter_tbl. root@ho ...

  6. 深度学习-05(tensorflow模型保存与加载、文件读取、图像分类:手写体识别、服饰识别)

    文章目录 深度学习-05 模型保存于加载 什么是模型保存与加载 模型保存于加载API 案例1:模型保存/加载 读取数据 文件读取机制 文件读取API 案例2:CSV文件读取 图片文件读取API 案例3 ...

  7. 2022-02-27:k8s安装yapi,yaml如何写?

    2022-02-27:k8s安装yapi,yaml如何写? 答案2022-02-27: yaml如下: apiVersion: apps/v1 kind: Deployment metadata: l ...

  8. 2022-02-14:k8s安装gogs,yaml如何写?

    2022-02-14:k8s安装gogs,yaml如何写? 答案2022-02-14: yaml如下: apiVersion: v1 kind: Service metadata: labels: a ...

  9. 2021-10-26:给定一个数组arr,arr[i] = j,表示第i号试题的难度为j。给定一个非负数M。想出一张卷子,对于任何相邻的两道题目,前一题的难度不能超过后一题的难度+M。返回所有可能的卷

    2021-10-26:给定一个数组arr,arr[i] = j,表示第i号试题的难度为j.给定一个非负数M.想出一张卷子,对于任何相邻的两道题目,前一题的难度不能超过后一题的难度+M.返回所有可能的卷 ...

  10. Spring Boot 配置文件总结

    前言 Spring Boot 中提供一个全局的配置文件:application.properties,这个配置文件的作用就是,允许我们通过这个配置文件去修改 Spring Boot 自动配置的默认值. ...