CodeForces - 1285D Dr. Evil Underscores(记忆化搜索+字典树)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1285D Dr. Evil Underscores(记忆化搜索+字典树)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出n個數字,現在要求出一個X,使得X與n個數字單獨異或之后的最大值,輸出這個最大值的最小值
題目分析:最大值的最小值,差點就以為是字典樹+二分了,因為沒有單調性然后就無從下手了,其實就是一個簡單的記憶化搜索,01字典樹維護信息后直接dfs就是答案了
代碼:
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;int trie[N*32][2],cnt=0;void insert(int x) {int pos=0;for(int i=31;i>=0;i--){int to=(x>>i)&1;if(!trie[pos][to])trie[pos][to]=++cnt;pos=trie[pos][to];} }int dfs(int step,int pos) {if(step==-1)return 0;if(!trie[pos][0])return dfs(step-1,trie[pos][1]);if(!trie[pos][1])return dfs(step-1,trie[pos][0]);return min(dfs(step-1,trie[pos][0]),dfs(step-1,trie[pos][1]))|(1<<step); }int main() { // freopen("input.txt","r",stdin);int n;scanf("%d",&n);while(n--){int num;scanf("%d",&num);insert(num);}printf("%d\n",dfs(31,0));return 0; }?
總結
以上是生活随笔為你收集整理的CodeForces - 1285D Dr. Evil Underscores(记忆化搜索+字典树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ - 2289 Jamie's C
- 下一篇: CodeForces - 1285E D