Educational Codeforces Round 134 (Rated for Div. 2) D Maximum AND
Maximum AND#
贪心
从高位开始,尽可能地让 中该位为 的 和 中该位为 的配对在一起,换句话说,可以让 由小到大排序, 由大到小排序
-
如果当前位最终是 ,则继续该过程
-
如果当前位最终是 ,则说明失配,考虑将这一位全部置为 (排除这一位对排序造成的影响),重新排序
整个过程有种递归的感觉
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
vector<int>a(n), b(n);
for(int i=0; i<n; i++) cin >> a[i];
for(int i=0; i<n; i++) cin >> b[i];
sort(a.begin(), a.end());
sort(b.begin(), b.end(), cmp);
ll ans = 0;
for(int i=30; i>=0; i--)
{
int f = (1 << 31) - 1;
for(int j=0; j<n; j++)
f &= a[j] ^ b[j];
if(f >> i & 1 ^ 1)
{
for(int j=0; j<n; j++)
{
a[j] |= 1 << i;
b[j] |= 1 << i;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end(), cmp);
}
else
ans |= 1 << i;
}
cout << ans << "\n";
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话