CF1851F - Lisa and the Martians
题目描述
Lisa was kidnapped by martians! It okay, because she has watched a lot of TV shows about aliens, so she knows what awaits her. Let's call integer martian if it is a non-negative integer and strictly less than , for example, when , the numbers , , are martian, and the numbers , , , are not.
The aliens will give Lisa martian numbers . Then they will ask her to name any martian number . After that, Lisa will select a pair of numbers ( ) in the given sequence and count . The operation means Bitwise exclusive OR, the operation means Bitwise And. For example, .
Lisa is sure that the higher the calculated value, the higher her chances of returning home. Help the girl choose such that maximize the calculated value.
solution
1、分析
对于公式 来讲,其中 是一个固定的答案, 只是对这个答案的调解。
对于 与 二进制的第 位。
-
假设均为 ,那么 的该位置上应该为 ,以确保 的第 为也为 ,使得答案最优。
-
假设均为 ,那么 的该位置上应该为 ,以确保 的第 为也为 ,使得答案最优。
-
如果第 位上的值不相等,那么不管怎么异或,它们的第 上永远是 ,最终答案的第 位也一定为 。
2、最大值(答案)
对于每个 来讲, 的个数是固定的。
那么,就要找到一个每一位上不相同的数最小的数作为 ,即 的最小值。
就能使得 作为其中答案的最大值。
考虑到最终答案为 的形式,所以求全局最小值即可。
这个地方用 01trie 解决即可。
3、 的值
既然已经求得了最终的答案,那么按照 中所讲,只将 的位置设为 即可。
code
scanf("%d%d", &n, &k);
for (int i = 1, j; i <= n; i ++ ) {
scanf("%d", &a[i]);
j = query_min(a[i]); // 查询 min{a[i]^a[j]} 的j的编号
if (j != -1 && ans >= (res = a[j] ^ a[i])) {
ans = res; xi = i; xj = j;
}
insert(i);
}
x = (~(a[xi] | a[xj])) & ((1 << k) - 1);
cout << xj << ' ' << xi << ' ' << x << '\n';
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】