[Bzoj4260]Codechef REBXOR(trie树)
生活随笔
收集整理的這篇文章主要介紹了
[Bzoj4260]Codechef REBXOR(trie树)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
4260: Codechef REBXOR
?
Time Limit:?10 Sec??Memory Limit:?256 MBSubmit:?1534??Solved:?669
[Submit][Status][Discuss]
Description
?
?
Input
?
輸入數據的第一行包含一個整數N,表示數組中的元素個數。 第二行包含N個整數A1,A2,…,AN。?
Output
?
輸出一行包含給定表達式可能的最大值。?
Sample Input
?
5 1 2 3 1 2?
Sample Output
?
6?
HINT
?
?
滿足條件的(l1,r1,l2,r2)有:(1,2,3,3),(1,2,4,5),(3,3,4,5)。對于100%的數據,2 ≤ N ≤ 4*105,0 ≤ Ai ≤ 109。
?
?
Source
?
By yts1999
?
分析:
用trie樹處理出每個點作為右端點or左端點所能取到異或最大值,枚舉分界點即可
?
復雜度O(n*30) 難道只有我做題的時候有點分不清左右還wa了一次嗎,路癡QAQAC代碼:
?
# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; const int N = 4e5 + 12; int ch[N * 32][2],dt,l[N],r[N],n,a[N],ret; void clear(int x){ch[x][0] = ch[x][1] = 0;} void insert(int x) {int p = 0,v;for(int i = 30;~i;i--){v = x >> i & 1;if(!ch[p][v])ch[p][v] = ++dt,clear(dt);p = ch[p][v];} } void init(){dt = 0;clear(0);insert(0);} int find(int x) {int ret = 0,v,p = 0;for(int i = 30;~i;i--){v = x >> i & 1;if(ch[p][v ^ 1]){ret += ((v ^ 1) << i);p = ch[p][v ^ 1];}else p = ch[p][v],ret += (v << i);}return ret ^ x; }int main() {scanf("%d",&n);for(int i = 1;i <= n;i++)scanf("%d",&a[i]);init();for(int i = 1;i <= n;i++)r[i] = max(r[i - 1],find(a[i])),insert(a[i]);init();for(int i = n;i >= 1;i--)l[i] = max(l[i + 1],find(a[i])),insert(a[i]);for(int i = 1;i < n;i++)ret = max(ret,l[i] + r[i + 1]);printf("%d\n",ret); }?
轉載于:https://www.cnblogs.com/lzdhydzzh/p/8776864.html
總結
以上是生活随笔為你收集整理的[Bzoj4260]Codechef REBXOR(trie树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络协议入门(OSI七层和TCP/IC四
- 下一篇: LeetCode初级算法(数组)解答