Atcoder Beginner Contest 236 F Spices
F. Spices
题目大意
从到 中选一些数出来,使得可以用这些数通过异或运算可以表示 到 中的任何数。选第 个数的代价为 ,最小化代价。
解题思路
肯定是线性基,我们贪心地从代价小的数开始插入线性基,能插就插,不能插就扔。这样最终代价是最小的。
至于证明,即插入四个数代价依次增大,能插 但我不插 反而插 的话,从最终结果考虑,我可以用和其他数表示出 ,同样 也可以用这些数和表示出 ,这样 就可以替换成 和那些数,同样能表示所有数。而 的代价更小,结果会更优。因此是能插就插。
神奇的代码
#include <bits/stdc++.h> using namespace std; typedef long long LL; int count(int x){ int cnt = 0; while(x){ cnt += (x & 1); x >>= 1; } return cnt; } int p[32]; bool insert(int x) { for (int i = 17; i + 1; i--) { if (!(x >> i)) // x的第i位是0 continue; if (!p[i]) { p[i] = x; return true; } x ^= p[i]; } return false; } int main(void) { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; LL ans = 0; vector<pair<LL,int> > qwq; for(int i = 1; i < (1 << n); ++ i){ LL x; cin >> x; qwq.push_back({x, i}); } sort(qwq.begin(), qwq.end()); for(auto i : qwq){ if (insert(i.second)) ans += i.first; } cout << ans << endl; return 0; }
本文作者:~Lanly~
本文链接:https://www.cnblogs.com/Lanly/p/15850262.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
标签:
,
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步