HDU 5536 Chip Factory 01trie
来源:互联网 发布:linux启动卡在进度条 编辑:程序博客网 时间:2024/06/08 09:51
题目:
http://acm.hdu.edu.cn/showproblem.php?pid=5536
题意:
给出
思路:
暴力居然可过。。。然后正解是用字典树去求最大异或值,首先把
#include <bits/stdc++.h>using namespace std;const int N = 1000 + 10, M = 2, INF = 0x3f3f3f3f;struct node{ node *next[M]; int val; void init() { memset(next, 0, sizeof next); val = 0; }}trie[N*32], *root;int tot;int len = 31;node *new_node(){ trie[tot].init(); return trie + tot++;}void trie_init(){ tot = 0; root = new_node();}void trie_insert(int x){ node *p = root; for(int i = len-1; i >= 0; i--) { int j = 1 & (x>>i); if(p->next[j] == NULL) p->next[j] = new_node(); p->next[j]->val++; p = p->next[j]; }}int trie_query(int x){ node *p = root; int ans = 0; for(int i = len-1; i >= 0; i--) { int j = ! (1 & (x>>i)); if(p->next[j] != NULL && p->next[j]->val != 0) { p = p->next[j]; ans = ans * 2 + 1; } else { p = p->next[!j]; ans = ans * 2 + 0; } } return ans;}void trie_del(int x){ node *p = root; for(int i = len-1; i >= 0; i--) { int j = 1 & (x>>i); p->next[j]->val--; p = p->next[j]; }}int main(){ int t, n; int a[N]; scanf("%d", &t); while(t--) { trie_init(); scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); trie_insert(a[i]); } int ans = 0; for(int i = 1; i <= n; i++) for(int j = i+1; j <= n; j++) { trie_del(a[i]); trie_del(a[j]); ans = max(ans, trie_query(a[i] + a[j])); trie_insert(a[i]); trie_insert(a[j]); } printf("%d\n", ans); } return 0;}
暴力代码:
#include <bits/stdc++.h>using namespace std;const int N = 1000 + 10, INF = 0x3f3f3f3f;int a[N];int main(){ int t, n; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); int ans = 0; for(int i = 1; i <= n; i++) for(int j = i+1; j <= n; j++) for(int k = j+1; k <= n; k++) { ans = max(ans, (a[i] + a[j]) ^ a[k]); ans = max(ans, (a[j] + a[k]) ^ a[i]); ans = max(ans, (a[i] + a[k]) ^ a[j]); } printf("%d\n", ans); } return 0;}
阅读全文
0 0
- hdu 5536 Chip Factory 01 Trie
- HDU 5536 Chip Factory (01Trie)
- HDU 5536 Chip Factory (枚举+01Trie)
- 01trie树 HDU 5536Chip Factory
- HDU 5536 Chip Factory 01trie
- HDU 5536 Chip Factory (暴力 或者 01Trie)
- HDU 5536 Chip Factory (暴力 或者 01Trie)
- hdu 5536 Chip Factory 01trie+可删除
- HDU-5536:Chip Factory(Trie树)
- HDU 5536 (ACM 2015 长春) Chip Factory [Trie树]
- hdu 5536 Chip Factory 2015长春区域赛 数据结构 Trie
- [HDU 5536] Chip Factory (trie树+异或)
- HDU 5536 Chip Factory ACM/ICPC 2015 Changchun(Trie)
- HDU 5536 Chip Factory——Trie + 异或运算
- hdu 5536 Chip Factory
- HDU 5536 Chip Factory
- 【HDU】 5536 Chip Factory
- HDU 5536 Chip Factory
- java如何获取需要在页面树形展示的json数组
- flex布局兼容总结
- 通过 MarqueeFactory 来提供各种样式的跑马灯 View, 支持自定义跑马灯 ItemView
- laravel+vue项目跳转拦截器
- Android音频驱动-ASOC之Codec
- HDU 5536 Chip Factory 01trie
- STL各种容器的使用总结
- android webView 全屏播放网络视频
- 基础博弈论之——简单的博弈问题【hdu1525】【Euclid‘s Game】
- 常见添加剂list
- 来自这里http://www.imooc.com/qadetail/149581?t=198457
- MATLAB显示串口接收的rgb565图像
- java中Logger.getLogger(Test.class)
- 文件操作时的问题