• ICPC 2015 Changchun现场赛--Chip Factory


    题目链接:http://exam.upc.edu.cn/problem.php?id=9264

    01字典树

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 int n,tot,trie[70005][2],a[1005],cnt[70005];
     5 void init(int i)
     6 {
     7     trie[i][1]=trie[i][0]=0;
     8     cnt[i]=0;
     9 }
    10 void add(int x,int id)
    11 {
    12     int now=0;
    13     for(int i=31;i>=0;i--)
    14     {
    15         int p=0;
    16         if(((1<<i)&x)!=0)   p=1;
    17         if(!trie[now][p])
    18         {
    19             tot++;
    20             trie[now][p]=tot;
    21             init(tot);
    22         }
    23         now=trie[now][p];
    24         cnt[now]+=id;
    25     }
    26 }
    27 int query(int x)
    28 {
    29     int now=0,tmp=0;
    30     for(int i=31;i>=0;i--)
    31     {
    32         int p=1;
    33         if(((1<<i)&x)!=0)   p=0;
    34         if(!trie[now][p]||cnt[trie[now][p]]<=0) now=trie[now][!p];
    35         else{
    36             tmp+=(1<<i);
    37             now=trie[now][p];
    38         }
    39     }
    40     return tmp;
    41 }
    42 int main()
    43 {
    44     int t;
    45     scanf("%d",&t);
    46     while(t--)
    47     {
    48         tot=0;
    49         init(tot);
    50         scanf("%d",&n);
    51         for(int i=1;i<=n;i++)
    52         {
    53             scanf("%d",&a[i]);
    54             add(a[i],1);
    55         }
    56         int ans=-1;
    57         for(int i=1;i<=n;i++)
    58         {
    59             for(int j=i+1;j<=n;j++)
    60             {
    61                 add(a[i],-1);
    62                 add(a[j],-1);
    63                 ans=max(ans,query(a[i]+a[j]));
    64                 add(a[i],1);
    65                 add(a[j],1);
    66             }
    67         }
    68         memset(cnt,0,sizeof(cnt));
    69         printf("%d
    ",ans);
    70     }
    71     return 0;
    72 }
    View Code
    如有错误,请指正,感谢!
  • 相关阅读:
    hdu1698(线段树)
    poj3468(线段树)
    hdu1394(线段树求逆序对)
    hdu1754(线段树)
    hdu1166(线段树)
    hdu2412(树形dp)
    hdu4714(树形dp)
    hdu4705(树形dp)
    hdu4679(树形dp)
    滑动导航条
  • 原文地址:https://www.cnblogs.com/scott527407973/p/9738781.html
Copyright © 2020-2023  润新知