Codeforces Round #875 (Div. 2) A-D
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的更多相关文章
- Codeforces Round #441 (Div. 2)【A、B、C、D】
Codeforces Round #441 (Div. 2) codeforces 876 A. Trip For Meal(水题) 题意:R.O.E三点互连,给出任意两点间距离,你在R点,每次只能去 ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- 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 ...
- 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 ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
随机推荐
- KK 与答辩
KK 与答辩 解读一下题:如果在所有场的答辩中,有某个人的总分都要低于kk的总分,就说kk碾压该人 --> 如果在某场答辩中这个人的总分大于kk,那么就说明kk不能碾压该人. 思路就清晰了,我们 ...
- Sentinel实战
一.Sentinel简介 Sentinel是阿里开源的面向服务流量治理的框架,官方原文是Sentinel 是面向分布式.多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由.流量控制.流 ...
- vite项目优化----- 解决终端optimized dependencies changed. reloading问题
写在前面网上都说vite要比webpack快,但个人感受,默认情况下, vite项目的启动确实比webpack快,但如果某个界面是首次进入,且依赖比较多/比较复杂的话,那就会比较慢了. 这篇文章就是用 ...
- [ Docker ] 部署 nps 和 npc 实现内网穿透
https://www.cnblogs.com/yeungchie/ 云主机上运行 nps 创建映射目录 mkdir -p ~/docker/nps/config 拉取镜像 docker pull o ...
- 快速上手Linux核心命令(五):文本处理三剑客
@ 目录 前言 正则表达式 第一剑客 grep 第二剑客 sed 第三 剑客 awk 小结 剑仙镇楼~ O(∩_∩)O 前言 上一篇中已经预告,我们这篇主要说Linux文本处理三剑客.他们分别是gre ...
- Win YAPI + Jenkins 实现接口自动化测试
自动化测试 传统的接口自动化测试成本高,大量的项目没有使用自动化测试保证接口的质量,仅仅依靠手动测试,是非常不可靠和容易出错的. 为了解决这个问题,使用YAPI接口自动化测试功能,只需要配置每个接口的 ...
- 微信小程序搜索不到腾讯服务路线规划插件的解决方法
具体操作如下: 提示:主要内容都是按开发文档来写的 开发文档: 链接: https://lbs.qq.com/miniProgram/plugin/pluginGuide/routePlan 添加插件 ...
- windows10下编译32位和64位webrtc(m77)静态库
1. windows10下编译32位和64位webrtc(m77)静态库 省略挂代理下载depot_tools以及webrtc代码的过程... 可参考webrtc编译 务必在 cmd 终端环境下进入到 ...
- python中的一些解码和编码
开头 最近爬取百度贴吧搜索页的时候遇到一个url的编码问题,颇为头疼,记录下来防止下次忘记 工具网站 解码编码的工具网站推荐 http://tool.chinaz.com/tools/urlencod ...
- 手机端调试工具vConsole
vConsole 一个轻量.可拓展.针对手机网页的前端开发者调试面板. vConsole 是框架无关的,可以在 Vue.React 或其他任何框架中使用. 现在 vConsole 是微信小程序的官方调 ...