"华为杯" 武汉大学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;
}
posted @ 2022-04-04 22:45  zyy2001  阅读(35)  评论(0编辑  收藏  举报