Team Formation 浙江省省赛原题

本文介绍了一个算法问题,探讨如何从N个学生中选择两名技能水平合适的成员组成参赛团队。问题的关键在于通过位运算判断两个成员技能相加后的总技能是否大于各自技能水平,以此来评估团队合作效果。

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

B - Team Formation
Time Limit:3000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

Description

For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-man team from N students of his university.

Edward knows the skill level of each student. He has found that if two students with skill level A and B form a team, the skill level of the team will be A ⊕ B, where ⊕ means bitwise exclusive or. A team will play well if and only if the skill level of the team is greater than the skill level of each team member (i.e. A ⊕ B > max{AB}).

Edward wants to form a team that will play well in the contest. Please tell him the possible number of such teams. Two teams are considered different if there is at least one different team member.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (2 <= N <= 100000), which indicates the number of student. The next line contains N positive integers separated by spaces. The ith integer denotes the skill level of ith student. Every integer will not exceed 109.

Output

For each case, print the answer in one line.

Sample Input

2
3
1 2 3
5
1 2 3 4 5

Sample Output

1
6

此题考察了很多位运算的知识。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int a[100005];
int b[50];//b[i]表示有多少个数转换为二进制后i是最高位 
void caculate(int num){
	int i=0;
	while(num>>1){
		i++;
		num=num>>1;
	}
	b[i]++;
	return ;
}//统计最高位为i的数目 
int main(){
	int cases,n,i,j,ans;
	scanf("%d",&cases);
	while(cases--){
		memset(b,0,sizeof(b));//初始化 
		ans=0;
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%d",&a[i]);
			caculate(a[i]);
		}
		for(i=0;i<n;i++){
			j=31;//就是32位下int的最大值 即2的31次方
			while(j>=0){
				if(a[i]&(1<<j))break;//找到和a[i]最高位相同的数 
				j--;
			}
			while(j>=0){
				if(!(a[i]&(1<<j))){ans+=b[j];}
				j--;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}



### 关于 Unity 中 Formation 的实现 在游戏开发领域,Formation 是指一组对象按照特定排列方式组成的结构化布局。这种技术广泛应用于策略类游戏、塔防游戏以及角色扮演游戏 (RPG) 等场景中。以下是关于如何在 Unity 中实现 Formation 的一些常见方法和技术。 #### 使用 Transform 和 Vector3 实现基本 Formation 通过调整 GameObjects 的位置来创建队列是一种简单而有效的方法。可以利用 `Transform` 组件和 `Vector3` 来定义目标物体的位置偏移量[^1]。例如: ```csharp public class FormationController : MonoBehaviour { public List<GameObject> members; // 队伍成员列表 public float spacingX = 2f, spacingY = 2f; // X轴和Y轴间距 void ArrangeInGrid() { int index = 0; foreach (var member in members) { int row = index / Mathf.Ceil(Mathf.Sqrt(members.Count)); // 行号计算 int col = index % Mathf.Floor(Mathf.Sqrt(members.Count)); // 列号计算 member.transform.position = new Vector3( transform.position.x + col * spacingX, transform.position.y + row * spacingY, transform.position.z); index++; } } private void Start() { ArrangeInGrid(); // 调用函数初始化网格布局 } } ``` 上述脚本实现了基于二维数组的 Formation 布局逻辑,并允许开发者自定义行列之间的距离参数。 #### 动态更新 Formation 如果希望 Formation 可以动态变化,则可以通过监听事件或者定时器重新调用布置算法。比如当有新单位加入队伍时触发一次新的排列过程: ```csharp void AddMember(GameObject newMember) { members.Add(newMember); // 添加到集合中 ArrangeInGrid(); // 更新整个阵型 } ``` #### 复杂 Formation 的实现 对于更复杂的 Formation(如圆形分布),可采用极坐标转换的方式设置每个节点的位置: ```csharp private void ArrangeInCircle(float radius) { for(int i=0;i<members.Count;i++) { float angleStep = ((float)i/(members.Count)) * 360f*Mathf.Deg2Rad; var positionOffset = new Vector3( Mathf.Cos(angleStep)*radius, Mathf.Sin(angleStep)*radius, 0); members[i].transform.localPosition = positionOffset; } } ``` 此代码片段展示了如何让多个对象围绕中心点形成圆环状排列。 --- ### 总结 以上介绍了几种常见的 Unity Formation 实现方案,包括简单的线性排列、网格布局以及更加灵活的圆形分布模式。每种形式都有其适用范围,在实际项目应用过程中可以根据需求选择合适的解决方案并加以优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值