1. 程式人生 > >HDU 4825 Xor Sum

HDU 4825 Xor Sum

getc ear 程序設計 ever 資格賽 script const sub select

Xor Sum

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 3633 Accepted Submission(s): 1590


Problem Description Zeus 和 Prometheus 做了一個遊戲,Prometheus 給 Zeus 一個集合,集合中包含了N個正整數,隨後 Prometheus 將向 Zeus 發起M次詢問,每次詢問中包含一個正整數 S ,之後 Zeus 需要在集合當中找出一個正整數 K ,使得 K 與 S 的異或結果最大。Prometheus 為了讓 Zeus 看到人類的偉大,隨即同意 Zeus 可以向人類求助。你能證明人類的智慧麽?

Input 輸入包含若幹組測試數據,每組測試數據包含若幹行。
輸入的第一行是一個整數T(T < 10),表示共有T組數據。
每組數據的第一行輸入兩個正整數N,M(<1=N,M<=100000),接下來一行,包含N個正整數,代表 Zeus 的獲得的集合,之後M行,每行一個正整數S,代表 Prometheus 詢問的正整數。所有正整數均不超過2^32。

Output 對於每組數據,首先需要輸出單獨一行”Case #?:”,其中問號處應填入當前的數據組數,組數從1開始計算。
對於每個詢問,輸出一個正整數K,使得K與S異或值最大。

Sample Input 2 3 2 3 4 5 1 5 4 1 4 6 5 6 3

Sample Output Case #1: 4 3 Case #2: 4

Source 2014年百度之星程序設計大賽 - 資格賽

Recommend liuyiding | We have carefully selected several similar problems for you: 6263 6262 6261 6260 6259 直接給數據跪了啊。 我有一個讀入用cin讀的,然後就T飛了。 對於這個題來說,對於每個元素,插到一顆0/1 Trie樹裏面, 對於讀入的數,在0/1 Trie樹上貪心的走,根據異或的原理,先走不同的,否則走相同的
// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long 
using namespace std;
const int MAXN=3500005;
const int INF=1e8+10;
inline int read()
{
    char c=getchar();int x=0,f=1;
    while(c<0||c>9){if(c==-)f=-1;c=getchar();}
    while(c>=0&&c<=9){x=x*10+c-0;c=getchar();}
    return x*f;
}
struct node
{
    int v,ch[2];
    node(){v=0;}
    void clear(){v=ch[0]=ch[1]=0;}
}T[MAXN];
int root=0,tot=0;
void Insert(int val)
{
    int now=root;
    for(int i=31;i>=0;i--)
    {
        int opt=(val&(1<<i))?1:0;
        if(T[now].ch[opt]==0) T[now].ch[opt]=++tot;
        now=T[now].ch[opt]; 
    }
    T[now].v=val;
}
int Query(int val)
{
    int now=root;
    for(int i=31;i>=0;i--)
    {
        int opt=(val&(1<<i))?1:0;
        if(T[now].ch[opt^1]) now=T[now].ch[opt^1];
        else                  now=T[now].ch[opt];
    }
    return T[now].v;
}
int main()
{
    int Test=read(),cnt=0;
    while( (++cnt)<=Test )
    {
        tot=0;root=0;
        int N=read(),M=read();
        for(int i=0;i<=MAXN;i++) T[i].clear();
        for(int i=1;i<=N;i++)
        {
            int p=read();
            Insert(p);
        }
        printf("Case #%d:\n",cnt);
        while(M--)
        {
            int p=read(); 
            printf("%d\n",Query(p));
        }
            
    }
    return 0;
}

HDU 4825 Xor Sum