Codeforces Round 975 (Div. 2)题解记录(A,B,E)

news/2024/9/28 1:46:58

这次头有点晕,A题2分钟秒了,B题想法对的,但是头晕一直没把式子写对,纯纯掉分了

A. Max Plus Size

暴力模拟

#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
#include<bitset>
#include<stack>
#include<algorithm>
#include<deque>
#include<random>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
ll gcd(ll x, ll y)
{if (y == 0)return x;elsereturn gcd(y, x % y);
}
ll ksm(ll x, ll y)
{ll ans = 1;while (y){if (y & 1)ans *= x;x *= x;y >>= 1;}return ans;
}
void fio()
{ios::sync_with_stdio();cin.tie(0);cout.tie(0);
}
ll a[250000];
int main()
{fio();ll t;cin >> t;while (t--){ll n;cin >> n;for (ll i = 1; i <= n; i++)cin >> a[i];ll k1 = 0, k2 = 0;ll ans = 0, cnt = 0;for (ll i = 1; i <= n; i++){if (i % 2 == 0){ans++; k1 = max(k1, a[i]);}elsecnt++, k2 = max(k2, a[i]);}cout << max(ans + k1, cnt + k2) << endl;}}

B. All Pairs Segments

不难,看懂题意就会了,求正好被扫过某个次数的给定区间数字的个数

#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
#include<bitset>
#include<stack>
#include<algorithm>
#include<deque>
#include<random>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
ll gcd(ll x, ll y)
{if (y == 0)return x;elsereturn gcd(y, x % y);
}
ll ksm(ll x, ll y)
{ll ans = 1;while (y){if (y & 1)ans *= x;x *= x;y >>= 1;}return ans;
}
void fio()
{ios::sync_with_stdio();cin.tie(0);cout.tie(0);
}
ll a[250000];
ll b[250000];
int main()
{fio();ll t;cin >> t;while (t--){ll n, q;cin >> n >> q;for (ll i = 1; i <= n; i++)cin >> a[i];map <ll, ll>f;ll ans = 0;for (ll i = 1; i <= n ; i++){f[n - i + (i - 1) * (n - i + 1)]++;if (i != n){f[i * (n - i)] += a[i+1] - a[i]-1;}}for (ll i = 1; i <= q; i++){cin >> b[i];}for (ll i = 1; i <= q; i++){cout << f[b[i]] << " ";}cout << endl;}}

E. Tree Pruning

直接暴力把所有长度先记录下来,
暴力所有留下长度的可能性,用\(dfs\)往上爬,通过看儿子是否全被标记再考虑是否往上爬,这样子就可以把短的边去掉了
\(Onlog(n)\)

#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
#include<bitset>
#include<stack>
#include<algorithm>
#include<deque>
#include<random>
using namespace std;
typedef long long ll;
ll ko = 0;
ll faa[500005];
void fio()
{ios::sync_with_stdio();cin.tie(0);cout.tie(0);
}
ll sz[500025];
vector<ll>g[500050];
set<ll>q[500050];
set <ll>f;
ll vis[550000];
ll dfs1(ll x)
{ll cnt = 0;if (x == 1)return 0;if (vis[x]==g[x].size()-1){vis[faa[x]]++;return dfs1(faa[x])+1;}elsereturn 0;
}
void dfs(ll x,ll fa,ll sd)
{sz[x] = 1;q[sd].insert(x);f.insert(sd);faa[x] = fa;for (auto j : g[x]){if (j == fa)continue;dfs(j, x,sd+1);sz[x] += sz[j];}return;
}
int main()
{fio();ll t;cin >> t;while (t--){ll n;cin >> n;f.clear();for (ll i = 1; i <= n; i++){faa[i] = 0;g[i].clear();sz[i] = 0;vis[i] = 0;q[i].clear();}for (ll i = 1; i <= n - 1; i++){ll l, r;cin >> l >> r;g[r].push_back(l);g[l].push_back(r);}dfs(1, 0, 0);for (ll i = 1; i <= n; i++)vis[i] = 0;ll ans = 0;ll u = 9999999999;ll ou = 0;ko = 0;for (auto j : f){ans = 0;if (j != 0){ou = ko;for (auto k : q[j]){ans += sz[k] - 1;if (sz[k] == 1){ko+=dfs1(k);}}u = min(u, ou + ans);}}cout << u << endl;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/65617.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Git 分支本质及与 commit、HEAD、tag 的关系

基于: Git - Git 是什么? Git - 分支简介 Git - 打标签快照 在介绍 Git 分支前,需要知道什么是 commit 对象,介绍 commit 对象前,需要先了解 Git 保存数据的方式。Git 直接记录快照,而非差异比较。 从概念上来说,其它大部分版本控制系统(包括 Subversion 和近似工具)以…

深度讲解-互联网算法备案指南和教程

随着人工智能和大数据技术的迅猛发展,互联网算法在内容推荐、用户画像、智能客服等领域发挥着越来越重要的作用。然而,算法的广泛应用也带来了潜在的安全风险和合规挑战。为了规范互联网算法的开发与应用,国家互联网信息办公室等相关部门发布了《互联网算法备案管理规定》,…

Git 分支本质及与 commit、HEAD、tag 之间的关系

基于: Git - Git 是什么? Git - 分支简介 Git - 打标签快照 在介绍 Git 分支前,需要知道什么是 commit 对象,介绍 commit 对象前,需要先了解 Git 保存数据的方式。Git 直接记录快照,而非差异比较。 从概念上来说,其它大部分版本控制系统(包括 Subversion 和近似工具)以…

k8s 分布式存储平台 -- Longhorn

目录一、什么是 Longhorn二、架构设计1、工作原理2、工作流程3、基于微服务设计的优势三、安装1、安装要求2、使用 Longhorn 命令行工具(验证方式一)3、使用环境检查脚本(验证方式之二)3.1、安装 jq3.2、运行脚本4、安装 open-iscsi4.1、SUSE 和 openSUSE4.2、Debian 和 Ub…

全网最适合入门的面向对象编程教程:53 Python 字符串与序列化-字符串与字符编码

在 Python 中,字符串是文本的表示,默认使用 Unicode 编码,这允许你处理各种字符集,字符编码是将字符转换为字节的规则,常见的编码包括UTF-8、UTF-16和ASCII。全网最适合入门的面向对象编程教程:53 Python 字符串与序列化-字符串与字符编码摘要: 在 Python 中,字符串是文…

【基础岛第3关】浦语提示词工程实践

[to2024-09-25 18:32:11 星期三c] 案例描述 0、前期准备 创建开发机 0.1 环境配置创建虚拟环境并激活创建虚拟环境conda create -n langgpt python=3.10 -y conda activate langgpt 2. 安装必要的库 # 安装一些必要的库 conda install pytorch==2.1.2 torchvision==0.16.2 torc…

9月27日swing知识点

swing是一系列图形用户界面的控件的集合 Swing中GUI类分为三大类: 容器类 JFrame、JPanel、JScrollPane UI组件类 JLabel、JTextField、JTextArea、JButton JCheckBox、JRadioButton、JComboBox 帮助类 Color、Font、Dimension 这三者的依存关系为组件必须依存在顶层容器中,组…