【HDU5536】Chip Factory(01字典树+01字典树上删除某个数)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5536
题目:
Chip Factory
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Problem Description
John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage large amounts of products, every processor has a serial number. More specifically, the factory produces n chips today, the i-th chip produced this day has a serial number si.
At the end of the day, he packages all the chips produced this day, and send it to wholesalers. More specially, he writes a checksum number on the package, this checksum is defined as below:
maxi,j,k(si+sj)⊕sk
which i,j,k are three different integers between 1 and n. And ⊕ is symbol of bitwise XOR.
Can you help John calculate the checksum number of today?
Input
The first line of input contains an integer T indicating the total number of test cases.
The first line of each test case is an integer n, indicating the number of chips produced today. The next line has n integers s1,s2,..,sn, separated with single space, indicating serial number of each chip.
1≤T≤1000
3≤n≤1000
0≤si≤10^9
There are at most 10 testcases with n>100
Output
For each test case, please output an integer indicating the checksum number in a line.
Sample Input
2
3
1 2 3
3
100 200 300
Sample Output
6 400
解题思路:
两层for遍历寻找i和j(i≠j),问题转化为01字典树的经典问题,在一堆数里找一个数s[k],使得s[k]和(s[i]+s[j])的异或值最大,但是题目又要求k,i,j互不相同,所以在i,j一定找k时,需要在01字典树上“删除”s[i],s[j]。
但是并非真的的删除,exist[]记录每个节点在建01字典树时被访问的次数,当要删除s[i]时, 只需要把s[i]在树上的节点的exist值都-1,这样就实现了“删除”s[i]。后,+1 即恢复。
在树上遍历贪心寻找s[k]时,ch[u][v^1]节点不仅要在第一次建树时存在,而且要删除s[i]和s[j]之后也必须存在(exist值不为0)才能贪心的跳转到ch[u][v^1]去。
注:树上的一个节点在第一次建树的时候可能会被多次访问。
ac代码:
时间:近4s
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1005;// 8!=40320!!不是10000
const int max_base = 32;
ll val[32*maxn], a[maxn];
int ch[32*maxn][2], exist[32*maxn];
int tol = 0;
void init()
{tol = 1;ch[0][0] = ch[1][0] = 0;
}
void insert(ll x)
{int u = 0;for(int i = max_base; i >= 0; i--){int v = (x >> i & 1);if(!ch[u][v]) // 新建节点{ch[tol][1] = ch[tol][0] = 0;val[tol] = 0; //不是数值节点exist[tol] = 0;//当前要确定的节点编号tolch[u][v] = tol++;}u = ch[u][v];exist[u]++;//可能会被访问多次}val[u] = x;
}
void update(ll x, int add)
{int u = 0;for(int i = max_base; i >= 0; i--){int v = x >> i & 1;u = ch[u][v];exist[u] += add;}
}
ll query(ll x)
{int u = 0;for(int i = max_base; i >= 0; i--){int v = x >> i & 1;if(ch[u][v^1] && exist[ch[u][v^1]]) u = ch[u][v^1]; // 贪心的找每位异或起来的1的节点, 且这个节点存在,可能其他数在该节点上有相应的值else u = ch[u][v];}return x^val[u];
}
int main()
{//freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);int t, n;scanf("%d", &t);while(t--){init();scanf("%d", &n);ll ans = 0;for(int i = 0; i < n; i++){scanf("%lld", &a[i]);insert(a[i]);}for(int i = 0; i < n; i++){for(int j = i + 1; j < n; j++){ll x = a[i] + a[j];update(a[i], -1);update(a[j], -1);//在01字典树上'删除'这两个数ans = max(ans, query(x));update(a[i], 1);update(a[j], 1);//恢复}}printf("%lld\n",ans);}return 0;
}
【HDU5536】Chip Factory(01字典树+01字典树上删除某个数)相关推荐
- 字典树-01字典树基础
字典树-01字典树 什么是字典树? 字典树,又叫前缀树,Trie树,通常被用作字符串匹配. 它的实现原理是什么? 先建立一颗树,对于这棵树上每个节点i与其后继节点间的连线,都有存入一个字符. 对于存图 ...
- Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或)
Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或) 题意: 3种操作: 1 插入一个数 2 删除一个数 3 给出一个数 ...
- 【字典树】字典树的创建(入门详细介绍)
Part one[何谓字典树] 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计. ...
- 字典树 01字典树【数据结构】
题目1:字典树 HDU 1251 统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以 ...
- 字典树01字典树专题对字典树的理解
对于字典树和01字典树的一点理解: 首先,字典树建树的过程就是按照每个数的前缀来的,如果你要存储一个全小写字母字符串,那么这个树每一个节点最多26个节点,这样的话,如果要找特定的单词的话,按照建树的方 ...
- 字典树01字典树算法笔记
1]学习了字典树之后,觉得它很明显的就是用空间来换时间,空间复杂度特别大,比如字典数单单存26个小写字母,那么每个节点的孩子节点都有26个孩子节点,字典树中的每一层都保留着不同单词的相同字母. 2]0 ...
- 字典树 01 字典树基础
Trie - 字典树 字典树这种数据结构最典型的用例就是存单词: 相较于普通的树结构O(logn)的时间复杂度,其时间复杂度为O(w),w为单词的长度: 基础代码 Trie中的节点Node是不存储字符 ...
- hust1350Trie【字典树+dfs || 字典树 + LCA】
大意:告诉你一些字符串 让你组成字典树, 然后定义每个节点到所有叶子节点的距离的和等于改点的value 当根节点只有一个孩子,该根节点也算一个叶子节点 问所有节点的value的最小值 分析: 开始做的 ...
- python 实现字典树_python字典树(Trie)的实现
class TrieNode: def __init__(self): self.nodes = dict() self.cnt = 0 self.length = 0 self.is_leaf = ...
- 字典树,01字典树,可持续化01字典树(总结+例题)
目录 字典树 01字典树 字典树例题: power oj 2390: 查单词 HDU 1671 Phone List HDU 1004Let the Balloon Rise HDU 1075 Wha ...
最新文章
- Linux环境下编写一个shell程序,此程序的功能:随机生成一个1-100的数(答案)让用户猜
- 2003 IIS搭建与配置
- Spring @Async注解
- 避免资源放在收藏夹里面吃灰的方法(如从typora上直接能导出html并且无缝连接到微信公众号的神奇网站)
- docker下交叉编译环境配置
- Centos7 下载、安装、配置、启动部署
- html div数据替换,在contenteditable div中替换innerHTML
- 【报告分享】中国老龄化社会的潜藏价值系列报告:第三篇章-银发经济的基本盘和新常态.pdf...
- com.alibaba.dubbo.rpc.RpcException: Failed to invoke remote method解决方法
- python自动化测试-Python自动化测试入门,看这一篇就足以
- 计算机图形学完整笔记(八):曲线曲面 - 2
- 中国智能座舱行业发展前景展望与投资战略规划研究报告2022年版
- 世界杯来了,移动办公的“世界杯”怎么踢?
- 【网络安全】SQL注入详细分析
- Android7.0 MTK 需求文档(一)
- java docx4j 工具和xdocreport工具创建PDF或者word报表
- GDB 的几个用法(until, finish, tui)
- jsp页面的iframe的用法
- 51nod快乐排队 1431
- 201310湛湛蓝天下的香山
热门文章
- 【SpringBoot】整合jdbc
- java switch程序_Java 基础分支语句之程序流程控制switch-case
- mysql内部联结_关于mysql的内部联结
- 单元格中指定内容标红_你一直都不知道,键盘上“F4”在office中的强大功能
- MyBatis generator 注解方式和xml方式
- deepin如何布署python_Dlib库教程(2):联合python的人脸检测、标记、识别
- c++ string split_闲话Python之砍瓜切菜split()
- [转]BAT 批处理脚本 教程
- Context 使用不当造成内存泄露
- Flash,EEPROM差别