题目描述

给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值。

输入

第一行一个正整数N。
接下来一行N个非负整数。

输出

一行,包含两个数,最大值和次大值。

样例输入

3
3 5 6

样例输出

6 5


题解

高斯消元求线性基裸题

由于线性基可以表示所有能够求出的异或和,所以我们只需要考虑线性基即可。

先求出线性基,然后按照从高位到低位的贪心思想来选择。

由于每个线性基的最高位在之前都没有出现过,所以每次选择一定会使答案增大,故直接从大到小选择即可。

#include <cstdio>
#include <algorithm>
using namespace std;
int a[100010];
int main()
{
int n , i , j , tot = 0 , ans = 0;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &a[i]);
for(i = 1 << 30 ; i ; i >>= 1)
{
for(j = ++tot ; j <= n ; j ++ )
{
if(a[j] & i)
{
swap(a[tot] , a[j]);
break;
}
}
if(j > n)
{
tot -- ;
continue;
}
for(j = 1 ; j <= n ; j ++ )
if(j != tot && a[j] & i)
a[j] ^= a[tot];
}
for(i = 1 ; i < tot ; i ++ ) ans ^= a[i];
printf("%d %d\n" , ans ^ a[tot] , ans);
return 0;
}

最新文章

  1. arrayLiist的四种遍历方法
  2. EF &ndash; 2.EF数据查询基础(上)查询数据的实用编程技巧
  3. iOS10 UI设计基础教程
  4. iOS之01-基本语法
  5. Android性能之启动时间篇
  6. WPF Window 服务安装
  7. .NET 中文转缩写拼音
  8. 服务器安装Apache+Tomcat+Memcached共享Session的构架设计
  9. const修饰的双重指针赋值解惑
  10. android音乐播放器开发 SweetMusicPlayer 智能负载直插式歌词
  11. .net导出不规则Excel
  12. canvas——随机生成迷宫
  13. springboot模块
  14. java - day005 - 数组工具类, 数组复制,二维数组,变量,方法, 面向对象
  15. RabbitMQ学习笔记(三) 发布与订阅
  16. 改善 C# 的语言习惯(一) - 使用属性而不是可访问的数据成员(整理中)
  17. Xcode9.2打包图片显示异常解决方案
  18. oracle SQL 执行顺序
  19. Spring 默认的 AopProxy
  20. es6(15)--generator

热门文章

  1. python爬虫之路——构造URL集
  2. Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS
  3. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C A Weakness and Poorness (三分)
  4. 【转】NSBundle的使用,注意mainBundle和Custom Bundle的区别
  5. Fruit Ninja(取随机数)
  6. python基础一 day15 内置函数
  7. CentOS安装RabbitMQ步骤
  8. linux - mysql 安装教程
  9. javaweb基础(10)_HttpServletRequest原理介绍
  10. Python——数据类型