B - Monkey King

本文描述了一个关于猴子间冲突解决的算法问题,猴子们通过决斗来解决争端,获胜者的力量会减半,最终形成一个力量分布网络。文章详细介绍了如何通过数据结构和算法来模拟这一过程,包括如何初始化猴子的强度值,处理冲突,更新力量值,以及合并群体。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can’t avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.

Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).

And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.
Input
There are several test cases, and each case consists of two parts.

First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).

Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.

Output
For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.
Sample Input
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
Sample Output
8
5
5
-1
10

一开始n只猴子都互相不认识,当两只猴子发生冲突时,两边认识的力量最大的猴子开始打架,打架后参与打架的猴子力量减半,并且两群猴子互相都认识了,询问每次冲突后两群猴子中力量最大的猴子。
题解很好理解,每次先把两群猴子中力量最大的拿出来,减半之后放回去,再将两个堆合并,取最大值,当一个结点只有一个子节点时,由于dis都是0!(虽然是左偏树但不一定子节点就是左儿子)所以要取两个儿子中存在的那个。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5; 
struct node  //定义结点 
{
    int val, lc, rc, dis, par;
} t[N];
void init(int x)   //结点初始化 
{
    for (int i = 1; i <= x; i++) 
	{
		scanf("%d",&t[i].val);
        t[i].lc = t[i].rc = t[i].dis = 0;
        t[i].par = i;
    }
}

int find(int x) 
{
    if (t[x].par == x) return x;
    else return t[x].par = find(t[x].par);
}
bool same(int x, int y) 
{
    return find(x) == find(y);
}

int merge(int x, int y) {
    if (x == 0)
        return y;
    if (y == 0)
        return x;
    if (t[x].val < t[y].val)
        swap(x, y);
    t[x].rc = merge(t[x].rc, y);
    t[t[x].rc].par = x;
    if (t[t[x].lc].dis < t[t[x].rc].dis)
        swap(t[x].lc, t[x].rc);
    if (t[x].rc == 0)
        t[x].dis = 0;
    else
        t[x].dis = t[t[x].rc].dis + 1;
    return x;
}
int pop(int x) 
{
    int l = t[x].lc, r = t[x].rc;
    t[x].lc = t[x].rc = t[x].dis = 0;
    t[l].par = l, t[r].par = r,t[x].par=x;
    return merge(l, r);
}
int n,m,a,b;
int main()
{
	while(~scanf("%d",&n))
	{
		init(n);
		scanf("%d",&m);
		while(m--)
		{		
			scanf("%d%d",&a,&b);
			if(find(a)==find(b))
			{
				printf("-1\n");
				continue;
			}
			
			int tmp=find(a);
			int lc=max(t[tmp].lc,t[tmp].rc);
			pop(tmp);
			t[tmp].val/=2;
			merge(tmp,find(lc));
			
			tmp=find(b);
			lc=max(t[tmp].lc,t[tmp].rc);		
			pop(tmp);
			t[tmp].val/=2;
			merge(tmp,find(lc));
			
			int ans=merge(find(a),find(b));
			
			printf("%d\n",t[ans].val);
		}
	}
} 
资源下载链接为: https://pan.quark.cn/s/abbae039bf2a 在计算机视觉领域,实时目标跟踪是许多应用的核心任务,例如监控系统、自动驾驶汽车和无人机导航等。本文将重点介绍一种在2017年备受关注的高效目标跟踪算法——BACF(Boosted Adaptive Clustering Filter)。该算法因其卓越的实时性和高精度而脱颖而出,其核心代码是用MATLAB编写的。 BACF算法全称为Boosted Adaptive Clustering Filter,是基于卡尔曼滤波器改进的一种算法。传统卡尔曼滤波在处理复杂背景和目标形变时存在局限性,而BACF通过引入自适应聚类和Boosting策略,显著提升了对目标特征的捕获和跟踪能力。 自适应聚类是BACF算法的关键技术之一。它通过动态更新特征空间中的聚类中心,更准确地捕捉目标的外观变化,从而在光照变化、遮挡和目标形变等复杂情况下保持跟踪的稳定性。此外,BACF还采用了Boosting策略。Boosting是一种集成学习方法,通过组合多个弱分类器形成强分类器。在BACF中,Boosting用于优化目标检测性能,动态调整特征权重,强化对目标识别贡献大的特征,从而提高跟踪精度。BACF算法在设计时充分考虑了计算效率,能够在保持高精度的同时实现快速实时的目标跟踪,这对于需要快速响应的应用场景(如视频监控和自动驾驶)至关重要。 MATLAB作为一种强大的数学计算和数据分析工具,非常适合用于算法的原型开发和测试。BACF算法的MATLAB实现提供了清晰的代码结构,方便研究人员理解其工作原理并进行优化和扩展。通常,BACF的MATLAB源码包含以下部分:主函数(实现整个跟踪算法的核心代码)、特征提取模块(从视频帧中提取目标特征的子程序)、聚类算法(实现自适应聚类过程)、Boosting算法(包含特征权重更新的代
内容概要:本书《Deep Reinforcement Learning with Guaranteed Performance》探讨了基于李雅普诺夫方法的深度强化学习及其在非线性系统最优控制中的应用。书中提出了一种近似最优自适应控制方法,结合泰勒展开、神经网络、估计器设计及滑模控制思想,解决了不同场景下的跟踪控制问题。该方法不仅保证了性能指标的渐近收敛,还确保了跟踪误差的渐近收敛至零。此外,书中还涉及了执行器饱和、冗余解析等问题,并提出了新的冗余解析方法,验证了所提方法的有效性和优越性。 适合人群:研究生及以上学历的研究人员,特别是从事自适应/最优控制、机器人学和动态神经网络领域的学术界和工业界研究人员。 使用场景及目标:①研究非线性系统的最优控制问题,特别是在存在输入约束和系统动力学的情况下;②解决带有参数不确定性的线性和非线性系统的跟踪控制问题;③探索基于李雅普诺夫方法的深度强化学习在非线性系统控制中的应用;④设计和验证针对冗余机械臂的新型冗余解析方法。 其他说明:本书分为七章,每章内容相对独立,便于读者理解。书中不仅提供了理论分析,还通过实际应用(如欠驱动船舶、冗余机械臂)验证了所提方法的有效性。此外,作者鼓励读者通过仿真和实验进一步验证书中提出的理论和技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值