牛客2025秋季算法编程训练联赛4-提升组

news/2025/11/5 22:26:14/文章来源:https://www.cnblogs.com/yuwj/p/19194819

写在前面

已经,无所谓了

C 题没看懂,不写了,F 只有一个过了,也不想写了

A

相当于就是填数字,总共的数字个数是 加号个数 + 1

然后发现要用高精度,完了

#include <bits/stdc++.h>
using namespace std;using i64 = long long;
using ll = long long;struct BigInt{vector<int> d;BigInt(){}BigInt(ll x){*this = x;}BigInt(string &s){ *this = s; }BigInt& operator=(const string &s){d.clear();for (int i = (int)s.size() - 1; i >= 0; --i) d.push_back(s[i] - '0');trim();return *this;}BigInt& operator=(ll x){d.clear();if(x==0){d.push_back(0); return *this;}while(x) {d.push_back(x%10), x/=10;}return *this;}void trim(){while(d.size()>1 && d.back() == 0) d.pop_back();}bool isZero() const {return d.size() == 1 && d[0] == 0;} static int cmp(BigInt A, BigInt B){A.trim(); B.trim();if(A.d.size() != B.d.size()) return A.d.size() < B.d.size() ? -1 : 1;int n = A.d.size(), m = B.d.size();for(int i = min(n,m)-1; i >= 0; --i){if(A.d[i] != B.d[i]) return A.d[i] < B.d[i] ? -1 : 1;}return 0;}
};BigInt operator*(const BigInt A, const BigInt B){if(A.isZero() || B.isZero()) return BigInt(0);BigInt res;res.d.assign(A.d.size() + B.d.size(),0);for(int i = 0; i<A.d.size(); ++i){int j=0, cur = 0, x = A.d[i];for(;j<B.d.size();++j){ll now = res.d[i+j] + x*B.d[j] + cur;res.d[i+j] = now%10, cur = now/10;}while(cur){ll now = res.d[i+j] + cur;res.d[i+j] = now%10;cur = now/10, ++j;}}res.trim();return res;
}BigInt operator+(const BigInt &A, const BigInt &B){BigInt res;int n = A.d.size(), m = B.d.size();int L = max(n, m), now = 0;res.d.resize(L);for (int i = 0; i < L; ++i){int x = now;if(i<n) x += A.d[i];if(i<m) x += B.d[i];res.d[i] = x % 10;now = x / 10;}if (now) res.d.push_back(now);return res;
}void solve(){string s; cin >> s;int num = 1; vector<int> cnt(10);for(auto ch : s){if(ch == '+') num++;else cnt[ch - '0']++;}vector<string> a(num+1);int idx = 1;for(int i = 1; i <= 9; ++i)if(cnt[i]){for(int j = 1; j <= cnt[i]; ++j){a[idx].push_back(char(i + '0'));idx = (idx + 1) % (num+1);if(idx == 0) idx = 1;}}// cerr << num << '\n';// 高精度,得要BigInt ans = 0;for(int i = 1; i <= num; ++i){if(a[i].size())ans = ans + BigInt(a[i]);// cerr << a[i] << '\n';}auto vec = ans.d;for(int i = vec.size() - 1; i >= 0; --i) cout << vec[i];
}signed main(){ios::sync_with_stdio(); cin.tie(0); cout.tie(0);int t = 1;// cin >> t;while(t--){solve();}return 0;
}

B

就是走距离为偶数的路径数量

定义: f[u]:以 u 为根节点的子树中奇数层节点数量,g[u]:以 u 为根节点的子树中偶数层节点数量

然后 dp[u]:表示以 u 为根节点的子树中偶数长度距离的节点对数,然后答案就对所有子树的方案求和完了

发现每个子树的贡献其实是所有点与兄弟节点的组合,

最后,注意是有序对

#include <bits/stdc++.h>
using namespace std;using i64 = long long;constexpr int Maxn = 1e6+10;
vector<int> G[Maxn];// 求一定能胜利的方案数
// 就是路径长度为偶数的路径数量 -> 点的层数奇偶分类,然后对子树 dp 计数完了
void solve(){int n;cin >> n;for(int i = 2; i <= n; ++i){int p; cin >> p;G[p].push_back(i), G[i].push_back(p);}vector<i64> f(n+1), g(n+1), dp(n+1);function<void(int,int)> dfs = [&](int u,int fa) -> void{vector<int> sons;f[u] = 1;int odd = 0, even = 0;for(auto v : G[u]){if(v == fa) continue;dfs(v,u);sons.push_back(v);f[u] += g[v], g[u] += f[v];odd += f[v], even += g[v];}for(auto v : sons){dp[u] += 1ll*f[v]*(odd - f[v]) + 1ll*g[v]*(even - g[v]);}dp[u] += (f[u]-1) * 2;};dfs(1,0);i64 ans = 0;for(int i = 1; i <= n; ++i) ans += dp[i];cout << ans << '\n';
}signed main(){ios::sync_with_stdio(); cin.tie(0); cout.tie(0);int t = 1;// cin >> t;while(t--){solve();}return 0;
}

D

就是询问的颜色区间内左右端点的组合方案数的和

考虑单点修改区间查询的线段树即可解决这个问题

template <class Info>
struct SegmentTree {struct Node {int l, r;Info info;};std::vector<Node> tr;SegmentTree() {};SegmentTree(int n) {init(n);}SegmentTree(std::vector<Info> & info) {init(info);}void init(int n) {tr.assign(n << 2, {});build(0, n - 1);}void init(std::vector<Info> & info) {int n = info.size();tr.assign(n << 2, {});std::function<void(int, int, int)> build = [&](int l, int r, int u) -> void {tr[u] = {l, r, {}};if (l == r) {tr[u].info = info[l];return;}int mid = l + r >> 1;build(l, mid, u << 1); build(mid + 1, r, u << 1 | 1);pushup(u);};build(0, n - 1, 1);}void pushup(int u) {tr[u].info = tr[u << 1].info + tr[u << 1 | 1].info;}void build(int l, int r, int u = 1) {tr[u] = {l, r, {}};if (l == r) {return;}int mid = l + r >> 1;build(l, mid, u << 1); build(mid + 1, r, u << 1 | 1);pushup(u);}void modify(int p, const Info & info, bool set = false) {int u = 1;while (tr[u].l != tr[u].r) {int mid = tr[u].l + tr[u].r >> 1;if (p <= mid) {u = u << 1;} else {u = u << 1 | 1;}}if (set) {tr[u].info = info;} else {tr[u].info = tr[u].info + info;}u >>= 1;while (u) {pushup(u);u >>= 1;}}Info query(int l, int r, int u = 1) {if (l <= tr[u].l && tr[u].r <= r) {return tr[u].info;}int mid = tr[u].l + tr[u].r >> 1;if (r <= mid) {return query(l, r, u << 1);} else if (l > mid) {return query(l, r, u << 1 | 1);}return query(l, r, u << 1) + query(l, r, u << 1 | 1);}
};struct Info{int sum;
};Info operator+(const Info &l, const Info &r){Info res;res.sum = l.sum + r.sum;return res;
}// 其实是颜色区间内不包含 i 位置颜色的三元组对数的和的数量
const int N = 5e5 + 10;
int cntl[N], cntr[N];
void solve(){int n;cin >> n;vector<T> num(n);for(auto &[col, l, r] : num) cin >> col >> l >> r, cntr[col]++;SegmentTree<Info> tr(N + 1);for(auto [col, l, r] : num){cntr[col]--;i64 x = 1ll * cntl[col] * cntr[col];tr.modify(col, {x}, 1);cout << tr.query(l,r).sum << ' ';cntl[col]++;x = 1ll * cntl[col] * cntr[col];tr.modify(col, {x}, 1);}
}

E

显然是 z = 1 类和 z = 0 类进行配对,

先排序 x,然后找 y,每次都找到最大的能够满足的 y 配对就能贪到最大匹配数量

可以用 multiset 完成这个事情

#include <bits/stdc++.h>
using namespace std;using i64 = long long;
using T = tuple<int,int,int>;void solve(){int n;cin >> n;vector<T> point(n);for(auto &[x,y,z] : point) cin >> x >> y >> z;sort(point.begin(), point.end());multiset<int> mst;int ans = 0;for(auto [x,y,z] : point){if(z == 1){auto it = mst.lower_bound(y);if(it != mst.begin()){++ans;--it; mst.erase(it);}}else{mst.insert(y);}}cout << ans << '\n';
}signed main(){ios::sync_with_stdio(); cin.tie(0); cout.tie(0);int t = 1;// cin >> t;while(t--){solve();}return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

11.05记录-机器学习

# 11.05记录 拟合 欠拟合 简单地说,就是模型学不会,模型太简单。例如,一个学生只会背公式,但是不会运用,题目稍微变形,就做不出来了。欠拟合说明模型只学会了表层的规律。 现象: 在训练集和测试集上表现都很差 …

Day14综合案例一--热词

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">…

机器学习-逻辑回归算法-基础数学原理版代码

`import numpy as np x=[[1,5],[1,7],[1,3],[1,3],[1,5],[1,6],[1,9],[1,8],[1,7],[1,6]] y=[0,1,0,0,0,1,1,1,1,1] num_x=len(x) num_fe=len(x[0]) 初始化权重和Z weight=[0.0 for _ in range (num_fe)] Z=[0.0 for _…

测试理论知识

一、 为什么要测试? 1、软件本身存在问题、非正常运行也会有问题 2、代码和文档是人写的,难免会出错 3、环境会影响软件 4、软件测试是保证质量的一种方法 二、测试的定义 制造业的定义: 以检验产品是否满足需求为目…

100小时学会SAP—问题1:FB50 做总账凭证时提示过账码没有定义

100小时学会SAP—问题1:FB50 做总账凭证时提示过账码没有定义利用FB50来做总账凭证提示“过账码 没有定义”。但是利用F-02做凭证没有提示相关错误,能正常做凭证。 解决方法如下: 转到事务码OBX1 (路径:SAP用户化…

模拟赛记录 11/5

前言:出分了,有点难受但又不多,毕竟已经难受过了。 T1贪心考虑 \(\text{顺序}-\text{顺序}\) 或 \(\text{顺序}-\text{逆序}\) 即可求出对应答案。点击查看代码 #include<bits/stdc++.h> using namespace std…

Win11 改虚拟内存到C盘之外的盘 - Leone

参考 https://answers.microsoft.com/zh-hans/windows/forum/all/win11%E6%97%A0%E6%B3%95%E6%8A%8A%E8%99%9A%E6%8B%9F/50381a46-ce77-40f5-8bde-a9d01b361e6c解决: WIN11无法把虚拟内存更改到其他盘,改完后重启显示…

随机数板子 - miao

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 这行代码用于创建一个高质量的随机数生成器。让我详细解释每个部分: 完整代码示例 #include <iostream> #include <random> #inc…

题解:P13933 [蓝桥杯 2022 省 Java B] 最大子矩阵

数据结构大于脑子发现标签是单调队列,我也不会单调队列,所以写 K-Dtree。 正文 一句话题意(迫真) 求所有满足矩形区域内最大值和最小值的差不大于 \(lim\) 的矩形区域的面积的最大值。 解析 那么思路就很清晰了。 …

深度学习非专业解释

深度学习非专业解释1.神经网络中,多个参数输入,与很多神经元进行组合,产生的结果根据权重等最终影响生成一个期望结果的事件。

内存管理-50-可读性-1-page_flags.h - Hello

内存管理-50-可读性-1-page_flags.h基于msm-5.4 实现下面 test.c, 然后 gcc -E test.c -o pp.c 然后得到 page_flags_my.h#define CONFIG_MMU#define CONFIG_SWAP //#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((…

25.11.05

AGC004E 运动是相对的,显然考虑挪动出口。 假设我们向四个方向最远移动分别是 \(u,d,l,r\),那么大矩形会从外往内删掉 \(d,u,r,l\)。 而注意到我们这个 \(u,d,l,r\) 框出的范围实质是个矩形,在这个矩形内造成的删除…

在React中实现路由跳转

在 React 中实现路由跳转可以使用多种方法,主要依赖于 react-router-dom 库。 常见的路由跳转方法 使用 useNavigate 钩子(适用于 react-router-dom v6): 1. useNavigate 是一个钩子,允许在函数组件中进行编程式导…

2025 11 4+11 5

11.4号这个只有一个上午的时间来着,晚上和中午我被弄去搞WHK了/jk vp 第十七场 T1 评分:绿直接秒了,倒着弄+并查集即可(用时20min)T2 评分:蓝这个我经过了一系列的思考才得出解法来着 首先我先观察到了 \(k_i \cdo…

022304105叶骋恺数据采集第二次作业

作业1 代码与运行结果 class WeatherForecast:def __init__(self):self.headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 6.0 x64; en-US; rv:1.9pre) Gecko/2008072421 Minefield/3.0…

2025.11.5模拟赛

赛时T1,T2看完都有锅,然后节奏有点乱了,改完题面后,写了T1,但是没太细想,所以出了很多问题,幸好写了一个拍,然后一边拍一边改错2h30min就过去了 调整了一下状态,T2看了一会,切了,但是考虑到一个细节,忘写了…

ai编程第一次实战

ai编程第一次实战安装uniapp的ide hbuilder, 配置nodejs环境, 安装trae ide hbuilder先创建新项目, 选择空白模板 ,项目文件名叫test trae打开这个项目test文件夹, 然后输入提示词. 用uniapp框架写一个留言板功能(不要…

WordPress Social Feed Gallery插件未授权信息泄露漏洞分析

本文详细分析了CVE-2025-10637漏洞,该漏洞影响WordPress Social Feed Gallery插件4.9.2及以下版本,由于缺少授权验证导致未认证攻击者能够窃取Instagram个人资料和媒体数据。概述 CVE-2025-10637是一个影响WordPress…

[题解]P14094 [ICPC 2023 Seoul R] Special Numbers

P14094 [ICPC 2023 Seoul R] Special Numbers 数位 DP。 考虑使用 \(f[pos][g]\) 记忆化,其中:\(pos\) 表示当前填到第几位。 \(g\) 表示填过位置的乘积与 \(k\) 的 \(\gcd\)。根据这个表格我们知道,\(10^{17}\) 内…