首页 > 其他分享 >The XOR Largest Pair

The XOR Largest Pair

时间:2023-06-12 21:33:58浏览次数:36  
标签:XOR int trie flag num Largest Pair bit include

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>

using namespace std;

int bit[32];
int n, num[5211314];

struct Trie {
	int trie[5211314][2], tot = 1;
	inline void Insert(int a) {
		int p = 1;
		for (int i = 30; i >= 0; -- i){
			//num[i] < 2^31 所以只用开到 30
			bool flag = (bit[i] & a);
			if (trie[p][flag] == 0) {
				trie[p][flag] = ++ tot;
			}
			p = trie[p][flag];
		}
	}
	inline int Query() {
		int final = 0;
		for (int i = 1; i <= n; ++ i) {
			int now = 0, p = 1;
			//遍历每一个数找最大maxn
			//因为异或是相同为1不同为0
			//所以在同一位置贪心找不一样的数(若num[i]为0则找1,若num[i]为1则找0)
			for (int j = 30; j >= 0; -- j) {
				bool flag = !(bit[j] & num[i]);
				if (trie[p][flag] != 0) {
					//若有不同的
					p = trie[p][flag];
					now += flag;
				}
				else {
					//若没有不相同的
					p = trie[p][!flag];
					now += (!flag);
				}
				if (j != 0) now <<= 1;
			}
			final = max(final, (num[i] ^ now));
			//更新
		}
		return final;
	}
}tree;

int main() {
	bit[0] = 1;
	for (int i = 1; i <= 31; ++ i) {
		bit[i] = (bit[i - 1] << 1);
	}
	scanf("%d", &n);
	for (int i = 1; i <= n; ++ i) {
		scanf("%d", &num[i]);
		//记录每一个数
		tree.Insert(num[i]);
	}
	cout << tree.Query() << endl;
	return 0;
} 

标签:XOR,int,trie,flag,num,Largest,Pair,bit,include
From: https://www.cnblogs.com/jueqingfeng/p/17476149.html

相关文章

  • Swap Nodes in Pairs
    SourceProblemGivenalinkedlist,swapeverytwoadjacentnodesandreturnitshead.ExampleGiven1->2->3->4,youshouldreturnthelistas2->1->4->3.ChallengeYouralgorithmshoulduseonlyconstantspace.Youmaynotmodifythe......
  • Java开发技巧-数据结构-使用HashSet判断主键是否存在、使用Pair成对结果返回/Triple三
    场景Java中使用HashSet判断主键是否存在HashSet实现Set接口,由哈希表(实际上是HashMap)实现,但不保证set的迭代顺序,并允许使用null元素。HashSet的时间复杂度跟HashMap一致,如果没有哈希冲突则时间复杂度为O(1),如果存在哈希冲突则时间复杂度不超过O(n)。所以,在日常编码中,可以使用HashSe......
  • Xor-MST
    Xor-MST这道题其实是一种最小生成树算法名曰Boruvka的算法,但是平时还是Kruskal算法用的说,相信大家也是由它想起的。根据套路,由于要求的是异或边权之和的最小值,果断构建01trie。我们将样例画出来我们可以对这颗树进行dfs,可以看出两个点的LCA越深,那么它们合并代价就越......
  • [ABC201E] Xor Distances 题解
    XorDistances题目大意给定一颗带边权无根树,定义\(\text{dis}(i,j)\)表示\(i,j\)两点在树上的最短路径的边权的异或和。求:\[\sum_{i=1}^n\sum_{j=i+1}^n\text{dis}(i,j)\]思路分析首先,容易证明:\[\text{dis}(i,j)=\text{dis}(i,x)\oplus\text{dis}(x,j)\]这个式子告诉我......
  • D. The BOSS Can Count Pairs
    D.TheBOSSCanCountPairsYouaregiventwoarrays$a$and$b$,bothoflength$n$.Yourtaskistocountthenumberofpairsofintegers$(i,j)$suchthat$1\leqi<j\leqn$and$a_i\cdota_j=b_i+b_j$.InputEachtestcontainsmultipletest......
  • leetcode 747. Largest Number At Least Twice of Others
    Inagivenintegerarraynums,thereisalwaysexactlyonelargestelement.Findwhetherthelargestelementinthearrayisatleasttwiceasmuchaseveryothernumberinthearray.Ifitis,returntheindexofthelargestelement,otherwisereturn-1.Ex......
  • leetcode Kth Largest Element in a Stream——要熟悉heapq使用
    703.KthLargestElementinaStreamEasyDesignaclasstofind thekthlargestelementinastream.Notethatitisthekthlargestelementinthesortedorder,notthekthdistinctelement.Your KthLargest classwillhaveaconstructorwhichacceptsanin......
  • 推断题(D - The BOSS Can Count Pairs)
    D-TheBOSSCanCountPairs#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;#defineendl"\n"//数学题关注边界条件和推断其他的值枚举算答案//nlogn做法//https://zhuanlan.zhihu.com/p/633006114//--------------------------------------------......
  • 2023CVPR_Learning a Simple Low-light Image Enhancer from Paired Low-light Instan
    一.motivation以前的大多数LIE算法使用单个输入图像和几个手工制作的先验来调整照明。然而,由于单幅图像信息有限,手工先验的适应性较差,这些解决方案往往无法揭示图像细节。二.contribution1.提出一个成对低光图像输入(相同内容,不同的曝光度)2.在输入之前进行了一个去噪操作,再......
  • 实现免杀:Shellcode的AES和XOR加密策略(vt查杀率:4/70)
    前言什么是私钥和公钥私钥和公钥是密码学中用于实现加密、解密和数字签名等功能的关键组件。私钥是一种加密算法中的秘密密钥,只有密钥的拥有者可以访问和使用它。私钥通常用于数字签名和数据加密等场景中,它可以用于对数据进行加密,同时也可以用于解密已经被加密的数据。公钥是......