"华为杯" 武汉大学21级新生程序设计竞赛

比赛链接

"华为杯" 武汉大学21级新生程序设计竞赛

D.和谐之树

求对区间 [1,n] 建立线段树后最大节点编号

解题思路

dfs

显然,构建的线段树为一棵完全二叉树,答案肯定位于深度最深且最靠右的节点上,考虑左右子树的深度,如果左子树深度大于右子树则排除右子树,否则排除左子树。注意,这里求深度由于递归求解的复杂度过高,需要迭代求,即每次转到长度较大的一半直到为 1 即得深度

  • 时间复杂度:O(tlogn)

代码

// Problem: 和谐之树 // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/31620/D // Memory Limit: 524288 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } int t; LL n,res; int deep(LL n) { int res=0; while(n>1) { n=(n+1)/2; res++; } return res; } void dfs(LL x,LL id) { res=max(res,id); if(x==1)return ; if(deep((1+x)/2)>deep(x-(1+x)/2))dfs((1+x)/2,id<<1); else dfs(x-(1+x)/2,id<<1|1); } int main() { for(scanf("%d",&t);t;t--) { scanf("%lld",&n); res=0; dfs(n,1); printf("%lld\n",res); } return 0; }

J.传闻档案

给定一张有向图,求每个点到其他能到的点(包括自己)的最大权值之和

解题思路

反向建边,dfs

考虑反向建边,将权值按从大到小排序,遍历节点遇到没有赋值的节点(说明前面较大权值的节点走不到该节点)直接赋上当前值

  • 时间复杂度:O(n+m)

代码

// Problem: 传闻档案 // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/31620/J // Memory Limit: 524288 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=1e5+5; vector<int> adj[N]; int n,m,f[N]; PII a[N]; void dfs(int x) { for(int y:adj[x]) { if(f[y])continue; f[y]=f[x]; dfs(y); } } int main() { cin>>n>>m; for(int i=1;i<=n;i++)cin>>a[i].fi,a[i].se=i; for(int i=1;i<=m;i++) { int x,y; cin>>x>>y; adj[y].pb(x); } sort(a+1,a+1+n); for(int i=n;i;i--) { if(f[a[i].se])continue; f[a[i].se]=a[i].fi; dfs(a[i].se); } LL res=0; for(int i=1;i<=n;i++)res+=f[i]; cout<<res; return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16100891.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示