首页 > 其他分享 >[ARC122D] XOR Game

[ARC122D] XOR Game

时间:2023-11-03 20:48:07浏览次数:24  
标签:ARC122D XOR int tr Alice Game ask integer Bob

Problem Statement

There are $2N$ integers written on a blackboard. The $i$-th integer is $A_i$.

Alice and Bob will play a game consisting of $N$ rounds. In each round, they do the following:

  • First, Alice chooses an integer on the blackboard and erases it. Let $x$ be the integer erased here.
  • Second, Bob chooses an integer on the blackboard and erases it. Let $y$ be the integer erased here.
  • Finally, write the value $x \oplus y$ on a notebook, where $\oplus$ denotes the bitwise XOR.

In the end, all the integers on the blackboard will be erased, and the notebook will have $N$ integers written on it. The greatest integer written on the notebook will be the score of the game. Alice wants to maximize this score, while Bob wants to minimize it. Find the score of the game when both players play optimally under their objectives.

Constraints

  • $1 \leq N \leq 2 \times 10^5$
  • $0 \leq A_i < 2^{30}$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\cdots$ $A_{2N}$

Output

Print the answer.


Sample Input 1

2
0 1 3 5

Sample Output 1

4

Below is one possible progress of the game (it may contain suboptimal choices).

  • Round $1$:

    • Alice chooses $A_1=0$.
    • Bob chooses $A_3=3$.
    • They write $0 \oplus 3=3$ on the notebook.
  • Round $2$:

    • Alice chooses $A_4=5$.
    • Bob chooses $A_2=1$.
    • They write $5 \oplus 1=4$ on the notebook.
  • The score of the game is $\max(3,4)=4$.


Sample Input 2

2
0 0 0 0

Sample Output 2

0

Sample Input 3

10
974654030 99760550 750234695 255777344 907989127 917878091 818948631 690392797 579845317 549202360 511962375 203530861 491981716 64663831 561104719 541423175 301832976 252317904 471905694 350223945

Sample Output 3

268507123

首先发现 Alice 是个废物,因为 Bob 找到原图一个匹配后, Alice 选什么 Bob 就选对应的那个即可。

所以现在要找到一个匹配,使得每条边两边的异或最大值最小。

考虑使用 trie 来分析。

对于某一个节点 \(x\) 的子树,如果他的左子树和右子树大小都是偶数,那么两个子树都可以自己完成匹配,可以递归到里面计算。

否则,就让他们自己匹配完之后,两个子树各自选出一个异或。所以可以递归计算两个子树中各选一个数异或的最小值。

#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5;
int n,a[N],ret=0,tr[N*30][2],mx=-1,tg[N*30],idx,s[30],sz[N*30];
void insert(int x)
{
	int u=0;
	for(int i=29;~i;--i)
	{
		if(!tr[u][x>>i&1])
			tr[u][x>>i&1]=++idx;
		u=tr[u][x>>i&1];
		sz[u]++;
	}
	tg[u]=x;
}
int ask(int x,int y)
{
	if(!x||!y)
		return INT_MAX;
	if(tg[x])
		return tg[x]^tg[y];
	if(!tr[x][0]&&!tr[y][1])
		return ask(tr[x][1],tr[y][0]);
	if(!tr[x][1]&&!tr[y][0])
		return ask(tr[x][0],tr[y][1]);
	return min(ask(tr[x][0],tr[y][0]),ask(tr[x][1],tr[y][1]));
}
void solve(int x)
{
	if(sz[tr[x][0]]&1)
		ret=max(ret,ask(tr[x][0],tr[x][1]));
	else
	{
		if(tr[x][0])
			solve(tr[x][0]);
		if(tr[x][1])
			solve(tr[x][1]);
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n*2;i++)
	{
		scanf("%d",a+i);
		for(int j=0;j<30;j++)
			if(a[i]>>j&1)
				s[j]++;
	}
	for(int i=1;i<=n*2;i++)
		insert(a[i]);
	solve(0);
	printf("%d",ret);
}

标签:ARC122D,XOR,int,tr,Alice,Game,ask,integer,Bob
From: https://www.cnblogs.com/mekoszc/p/17808408.html

相关文章

  • 2023PKU GeekGame Web wp
    2023PKUGeekGameWebwp第三新XSS巡猎查看源码我们可以知道可以在body的部分插入代码触发xss漏洞,根据题目给出的提示可以知道需要创建一个元素指向/admin/路径,然后通过document读取目标的cookies。<iframesrc="/admin/"id="barframe"></iframe><script>setTimeout(()=>......
  • Steam糖果派对新作《鼠托邦》BBGAMES建设老鼠王国的战略模拟电子游戏
    游戏《鼠托邦Ratopia》由独.立游戏开发团队CasselGames精心打造,将在11月6日起BBIN游戏抢先体验测试。在这款游戏中,您将化身为糖果派对游戏中的老鼠女王,领您的老鼠民众建设城市、勘探地.下领域以扩展生存空间。同时,您有机会根据不同老鼠市民的性格和技能,智慧地分配工作,依靠整......
  • CF1764D Doremy's Pegging Game 组合数学
    CF1764DDoremy'sPeggingGame你怎么连简单题也不会?考虑满足条件当且仅当有连续的n/2向下取整段被删除。考虑最终状态一定是一次删除联通了两个连续段,然后结束。我们枚举这个连续段的长度i。最后一个删除的位置有n/2下取整*2-i种方案,设另外删除了j种,则另外删除的方案......
  • CF1889D. Game of Stacks
    啊啊啊每次补完题都感觉这题我场上应该是能想出来的啊!考虑简化问题:若每个栈内只有一个元素,how。此时我们发现所有栈之间构成了一个内向基环树。且环是没有用的,因为我们在环上走一圈之后仍然会回到原点。所以不妨把所有环边删除,此时每棵树的答案即为树根。而对于原问题,同理,我们......
  • XOR 加密
    1.代码#include<stdio.h>#include<stdlib.h>#include<time.h>intmain(){srand(time(NULL));inta,b,c,i,n;longlongd=0;printf("原文:");scanf("%d",&a);printf("密钥长度:");sc......
  • BLOXORZ
    1关密码:780464→↓↓→→↓→2关密码:290299↑→↓→→→→↑↑↓↓→→→→↑←↑3关密码:918660→↑→→→↑→↓←↑↑→↓←←↑→→→→↓↓↓→↑4关密码:520967↑←↑→→↑→→→→→→↓→↓↓↓↓↓→↑←←←←←←↓5关密码:028431......
  • 用Python的Pygame包做飞行棋
    最近学了下pygame,感觉非常有意思,于是用自己的理解纯手工敲了几个游戏,下面记录一下我做飞行棋的思路过程: 运行结果玩家轮流投骰子然后移动飞机,全程只用鼠标操作,右上方会提示当前的轮次及操作 基础设置1)首先是导包和初始化一些变量,定义SIZE=40表示长方形的......
  • Unity中GameObject对象的方法Find,FindGameObjectsWithTag等API的使用方法
    Unity中GameObject对象的方法Find,FindGameObjectsWithTag等API的使用方法.Find(stringname):.FindGameObjectsWithTag(stringtag):.FindGameObjectWithTag(stringtag):.FindWithTag(stringtag):在Unity中,GameObject类具有一些用于查找和操作游戏对象的方法。.Find(stringna......
  • CF1854E Games Bundles 题解
    乱搞题设个\(dp[i]\)表示和为\(i\)的子序列个数,那么转移是容易的,\(dp[j]+=dp[j-i]\),然后就判下\(dp[60]+dp[60-i]\)是否大于\(m\),发现这样子搞对于比较大的数可能达不到\(m\)的限制,因为这样子转移,默认的是一个数只选一次,但是我们可以重复选,这启发我们需要设定一个值......
  • golang之xorm简单使用
    gogetgithub.com/go-xorm/xormpackagemainimport("fmt"_"github.com/go-sql-driver/mysql""github.com/go-xorm/xorm")typePointInfostruct{Idint64`xorm:"pkautoincr"`Product......