牛客小白月赛29 D 种树

D 种树

很久没来水博客了,来水一下博客
这个题目一开始还不知道怎么写但是仔细想了一下我们用max次数越多,那么最后的结果就越大,而最后的结果显然和最多能取多少次最大值,还有想要取的那个数的深度有关,如果最大值的深度小于可以取最大值的次数,那么就能取到,如果最大值的深度小于可以取最大值的次数,那么最后的结果就是最多能取最大值值的深度的最大值和以及这之外的最小值的最大值取max;(其实就是找到一个想要的值当深度小于可以取max的的时候一直取max,其他取min,找到所有情况然后取所有情况的max)

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<deque>
#include<map>
#include<stdlib.h>
#include<set>
#include<iomanip>
#include<stack>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define ull unsigned long long
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define endl "\n"
#define bug cout<<"----acac----"<<endl
#define IOS ios::sync_with_stdio(false), cin.tie(0),cout.tie(0)
using namespace std;
const int maxn = 5e5 + 10;
const int maxm = 1e4 + 50;
const double eps = 1e-7;
const ll inf = 0x3f3f3f3f;
const ll  lnf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
int n;
int a[maxn];
vector<int>ve[maxn];
int m, k;

int dfs(int u, int deep)
{
	int ans = -inf;
	if ((int)ve[u].size() == 0 && deep <= k)
	{
		ans = max(ans, a[u]);
		return ans;
	}
	else if (ve[u].size() == 0 && deep > k)
	{
		ans = min(ans, a[u]);
		return ans;
	}
	for (int i = 0; i < ve[u].size(); i++)
	{
		if (deep <= k)//当深度小于取max
		{
			ans = max(ans, dfs(ve[u][i], deep + 1));
		}
		else
		{
			ans = min(ans, dfs(ve[u][i], deep + 1));
		}
	}
	return ans;
}
int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		if (x && y)
		{
			ve[i].push_back(x);

			ve[i].push_back(y);
		}
	}
	for (int i = 1; i <= n; i++)scanf("%d", &a[i]);
	m = n >> 1;//一共需要剪枝多少次
	k = (m + 1) >> 1;//可以取最多取多少次max
	printf("%d\n",dfs(1, 0));
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值