[USACO 2015 Feb Silver P3] Superbull

文章讲述了Bessie和朋友们参与的Superbull淘汰赛,目标是通过安排比赛,使所有比赛的总分数最大化。关键在于利用团队ID的按位异或特性,通过Kruskal算法构建最大生成树来确定比赛顺序。
摘要由CSDN通过智能技术生成

总时间限制: 

10000ms

单个测试点时间限制: 

1000ms

内存限制: 

262144kB

描述

Bessie and her friends are playing hoofball in the annual Superbull championship, and Farmer John is in charge of making the tournament as exciting as possible. A total of NN (1 <= N <= 2000)(1<=N<=2000) teams are playing in the Superbull. Each team is assigned a distinct integer team ID in the range 1...2^30-1 to distinguish it from the other teams. The Superbull is an elimination tournament -- after every game, Farmer John chooses which team to eliminate from the Superbull, and the eliminated team can no longer play in any more games. The Superbull ends when only one team remains.

Farmer John notices a very unusual property about the scores in matches! In any game, the combined score of the two teams always ends up being the bitwise exclusive OR (XOR) of the two team IDs. For example, if teams 12 and 20 were to play, then 24 points would be scored in that game, since 01100 XOR 10100 = 11000.

Farmer John believes that the more points are scored in a game, the more exciting the game is. Because of this, he wants to choose a series of games to be played such that the total number of points scored in the Superbull is maximized. Please help Farmer John organize the matches.

中文翻译

Bessie和她的朋友们正在一年一度的Superbull锦标赛中打球,而Farmer John负责让比赛尽可能激动人心。总共有 N 支队伍( 1≤N≤2000 )参加了Superbull锦标赛。每个团队都有一个 1 ... 230−1的团队ID。

Superbull是一场淘汰赛 - 在每场比赛之后,FJ选择淘汰其中一支球队,而被淘汰的球队再也无法参加任何比赛了。当只剩下一支球队时,比赛就结束了。

FJ注意到一个不寻常的事情:在任何游戏中,两个团队的总分是两个团队ID的按位异或(XOR)。例如,如果第 12 队和第 20 队将参加比赛,则该游戏的得分为 24 分,因为01100 XOR 10100 = 11000。

FJ想要设计一种比赛方案,让所有比赛的得分总和最大。请帮助Farmer John组织比赛。

输入格式:第一行包括一个整数 N ,下面 N 行每行包括一个队伍的ID。

输出格式:输出一个整数,代表比赛的最大总得分。

样例解释:让 3 队与 9 队进行比赛,并让 9 队晋级。然后他让 6 队和 9 队对决,让 6 队获胜。最后,第 6 队和第 10 队比赛, 10 队获胜。总得分为:3 XOR 9+6 XOR 9+6 XOR 10=10+15+12=37。

输入

The first line contains the single integer N. The following N lines contain the N team IDs.

输出

Output the maximum possible number of points that can be scored in the Superbull.

样例输入

4
3
6
9
10

样例输出

37

因为是一场淘汰赛,所以是进行了n-1轮,所以我们很容易想到这是一个最大生成树,所以我们可以暴力n^2建边+kruskal或者直接n^2prim,边权就是两支队伍的id异或值。

代码:(kruskal)

#include<bits/stdc++.h> 
#define endl '\n'
#define lson (p<<1)
#define rson (p<<1|1)
#define ll long long
using namespace std; 
const int maxn=2e4+10,maxm=2e6+1e4;
const ll inf=1e18+7;
struct T{
	int u,v,w;
}e[maxm];
int n,m,a[maxn];
int fa[maxn],sz[maxn];
ll ans;
bool cmp(const T &x,const T &y){
	return x.w>y.w;
}
int find(int x){
	return x==fa[x]?x:fa[x]=find(fa[x]);
}
bool merge(int x,int y){
	x=find(x),y=find(y);
	if(x==y) return 0;
	if(sz[x]>sz[y]) swap(x,y);
	fa[x]=y;
	sz[y]+=sz[x];
	return 1;
}
void kruskal(){
	int tot=0;
	for(int i=1;i<=m;i++){
		int u=e[i].u,v=e[i].v,w=e[i].w;
		if(merge(u,v)){
			ans+=w;
			++tot;
		}
		if(tot==n-1) break;
	}
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i],fa[i]=i,sz[i]=1;
	for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) e[++m]={i,j,a[i]^a[j]};
	sort(e+1,e+1+m,cmp);
	kruskal();
	cout<<ans<<endl;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值