Superbull

时间限制: 1 Sec  内存限制: 64 MB
提交: 49  解决: 13
[提交][状态][讨论版]

题目描述

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 N (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 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

提示

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)

【分析】最大生成树,模板题

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
struct Edg {
int v,u;
ll w;
} edg[];
bool cmp(Edg g,Edg h) {
return g.w>h.w;
}
int n,m,maxn,cnt;
int parent[N];
int a[N];
void init() {
for(int i=; i<n; i++)parent[i]=i;
}
void Build() {
int u,v;
for(int i=; i<n; i++) {
scanf("%d",&a[i]);
}
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
edg[++cnt].u=i;
edg[cnt].v=j;
edg[cnt].w=a[i]^a[j];
}
}
sort(edg,edg+cnt+,cmp);
}
int Find(int x) {
if(parent[x] != x) parent[x] = Find(parent[x]);
return parent[x];
}
void Union(int x,int y) {
x = Find(x);
y = Find(y);
if(x == y) return;
parent[y] = x;
}
void Kruskal() {
ll sum=;
int num=;
int u,v;
for(int i=; i<=cnt; i++) {
u=edg[i].u;
v=edg[i].v;
if(Find(u)!=Find(v)) {
sum+=edg[i].w;
num++;
Union(u,v);
}
if(num>=n-) {
printf("%lld\n",sum);
break;
}
}
}
int main() {
scanf("%d",&n);
cnt=-;
init();
Build();
Kruskal();
return ;
}

最新文章

  1. mybatisGenerator 代码自动生成报错 Result Maps collection already contains value for BaseResultMap
  2. 【SFTP】使用Jsch实现Sftp文件下载-支持断点续传和进程监控
  3. UVA 11021 C - Tribles(概率DP)
  4. CentOS-6 yum安装nginx php53 mysql55 搭建LNMP环境
  5. centos7中systemctl命令使用方法和心得体会
  6. 设计模式-代理模式(Proxy)
  7. MVC 5 的 EF6 Code First 入门
  8. 实现js的类似alert效果的函数
  9. ExtJs中处理时间,出现NaN-NaN-NaN的解决方式
  10. 64位Window操作系统下,Orcal数据访问服务器端和客户端版本对应与通讯问题
  11. redis 安装启动及设置密码&lt;windows&gt;
  12. python计算文件夹大小(linux du命令 简化版)
  13. 201521123072《Java程序》第二周总结
  14. python 3.x 爬虫基础---Urllib详解
  15. saltstack主机管理项目:主机管理项目需求分析(一)
  16. Python 字符串增删改查的使用
  17. angluarjs中页面初始化的时候会出现语法{{}}在页面中问题
  18. axure交互样式(下拉列表和矩形)
  19. C. 【UNR #2】黎明前的巧克力
  20. jQuery.Ajax()执行WCF Service的方法

热门文章

  1. [NOI.AC省选模拟赛3.30] Mas的童年 [二进制乱搞]
  2. BZOJ4008 [HNOI2015]亚瑟王 【概率dp】
  3. C&amp;C++——C函数与C++函数相互调用问题
  4. spring中Constructor、@Autowired、@PostConstruct的顺序【转】
  5. [HNOI2003]消防局的设立 (贪心)
  6. POJ3189:Steady Cow Assignment(二分+二分图多重匹配)
  7. 数据结构之DFS与BFS
  8. webpack3基础知识
  9. Python 进阶学习笔记
  10. MDK stm32 仿真