12th浙江省省赛 Team Formation

本文探讨了一个算法问题,即如何从N名学生中选择两名技能水平合适的成员组成一支参赛队伍。队伍的技能水平由成员技能水平的按位异或运算决定,并且只有当队伍的技能水平高于每个成员的个人技能水平时,这支队伍才能发挥出色。文章提供了一种解决方案,通过数位dp或寻找规律的方法来计算可能的优秀队伍数量。
Team Formation

Time Limit: 3 Seconds      Memory Limit: 131072 KB

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 AB, 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. AB > max{A, B}).

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
思路:数位dp 或者 找规律:)
把异或表打出来 找数与数之间的关系
 
#include<bits/stdc++.h>
using namespace std;
int num[100020];
int sum[100];
int main()
{
    //freopen("b.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        memset(sum,0,sizeof(sum));
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d",&num[i]);
        sort(num,num+n);
        int ans=0;
        for(int i=0; i<n; i++)
        {
            int a=num[i];
            int wei=0;
            while(a)
            {
                a/=2;
                wei++;
            }
            wei--;
            //cout<<"--->"<<wei<<endl;
            int k=1;
            for(int j=0; j<wei; j++)
            {
                int chu=num[i]/k;
                if(chu%2==0)
                    ans+=sum[j];
                //cout<<i<<"   "<<j<<"  "<<sum[j]<<"  "<<ans<<endl;
                k*=2;
            }
            //cout<<"1."<<i<<"   "<<wei<<"  "<<sum[wei]<<endl;
            sum[wei]++;
            //cout<<"2."<<i<<"   "<<wei<<"  "<<sum[wei]<<endl;
        }
        cout<<ans<<endl;
    }
    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 实现方案,包括简单的线性排列、网格布局以及更加灵活的圆形分布模式。每种形式都有其适用范围,在实际项目应用过程中可以根据需求选择合适的解决方案并加以优化。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值