题意翻译
Bessie和她的朋友们正在一年一度的Superbull锦标赛中打球,而Farmer John负责让比赛尽可能激动人心。总共有 NN 支队伍( 1≤N≤20001≤N≤2000 )参加了Superbull锦标赛。每个团队都有一个 1 ... 2 ^ {30} -11...230−1的团队ID。
Superbull是一场淘汰赛 - 在每场比赛之后,FJ选择淘汰其中一支球队,而被淘汰的球队再也无法参加任何比赛了。当只剩下一支球队时,比赛就结束了。
FJ注意到一个不寻常的事情:在任何游戏中,两个团队的总分是两个团队ID的按位异或(XOR)。例如,如果第 1212 队和第 2020 队将参加比赛,则该游戏的得分为 2424 分,因为01100 XOR 10100 = 11000。
FJ想要设计一种比赛方案,让所有比赛的得分总和最大。请帮助Farmer John组织比赛。
输入格式:第一行包括一个整数 NN ,下面 NN 行每行包括一个队伍的ID。
输出格式:输出一个整数,代表比赛的最大总得分。
样例解释:让 33 队与 99 队进行比赛,并让 99 队晋级。然后他让 66 队和 99 队对决,让 66 队获胜。最后,第 66 队和第 1010 队比赛, 1010 队获胜。总得分为:3 XOR 9+6 XOR 9+6 XOR 10=10+15+12=37。
题目描述
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.
输入格式
The first line contains the single integer NN. The following NN 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
说明/提示
OUTPUT DETAILS: One way to achieve 37 is as follows: FJ matches teams 3 and 9, and decides that 9 wins, so teams 6, 9, and 10 are left in the tournament. He then matches teams 6 and 9, and lets team 6 win. Teams 6 and 10 are then left in the tournament. Finally, teams 6 and 10 face off, and team 10 wins. The total number of points scored is (3 XOR 9) + (6 XOR 9) + (6 XOR 10) = 10 + 15 + 12 = 37.
NOTE: The bitwise exclusive OR operation, commonly denoted by the ^ symbol, is a bitwise operation that performs the logical exclusive OR operation on each position in two binary integers. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but is 0 if both bits are 0 or both are 1. For example: 10100 (decimal 20) XOR 01100 (decimal 12) = 11000 (decimal 24)
[Problem credits: Alex Yang, 2015
题解:这是一道最大生成树的问题。在最小生成树的基础上改cmp即可。注意要开long long
#include<iostream> #include<algorithm> #include<queue> #include<cmath> #include<cstring> #include<cstdlib> #include<cstdio> using namespace std; typedef long long ll; const int N=2005; int n,m,cp,tot,fa[N],b[N]; ll c[N]; struct YCLL{ int u,v; ll va; }e[N*N]; int ans=0; bool cmp(YCLL aa,YCLL bb){ return aa.va>bb.va; } int find(int x){ if(fa[x]==x) return x; return fa[x]=find(fa[x]); } int op; ll Yao_Chen_Lai_Le(){ int tot=0; ll ans=0; for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=m;i++){ int uu=find(e[i].u); int vv=find(e[i].v); if(uu==vv) continue; ans+=e[i].va; fa[uu]=vv; tot++; if(tot==(n-1)) break; } //if(tot==(n-1)) return ans; //else return -1; return ans; } int main(){ freopen("4826.in","r",stdin); freopen("4826.out","w",stdout); scanf("%d ",&n); //for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=n;i++) scanf("%lld",&c[i]); for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ m++; e[m].u=i; e[m].v=j; e[m].va=c[i]^c[j]; } } sort(e+1,e+m+1,cmp); cout<<Yao_Chen_Lai_Le()<<endl; //for(int i=1;i<=m;i++) // printf("%d\n",Yao_Chen_Lai_Le(i)); return 0; }