HDU 5536 Chip Factory 字典树+贪心

给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k

思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多都用字典树,也是醉了

/*Problem : 5536 ( Chip Factory )     Judge Status : Accepted
RunId : 15506230    Language : G++    Author : qianbi08*/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include <algorithm>
#include<cstring>
using namespace std;
const int maxn=300005;
int node[maxn][2],cnt,re[maxn];
int a[1005];
int newnode()
{
    ++cnt;
    node[cnt][0]=node[cnt][1]=-1;
    re[cnt]=0;
    return cnt;
}
void update(int v,int d)
{
    int u=0;
    for(int i=30; i>=0; --i)
    {
        int c=(v>>i)&1;
        if(node[u][c]==-1)
            node[u][c]=newnode();
        u=node[u][c];
        re[u]+=d;
    }
}
int getans(int v)
{
    int u=0,ans=0;
    for(int i=30; i>=0; --i)
    {
        if(u==-1)break;
        int c=(v>>i)&1;
        if(node[u][c^1]!=-1&&re[node[u][c^1]])
        {
            ans=(ans|(1<<i));
            u=node[u][c^1];
        }
        else u=node[u][c];
    }
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        cnt=0;
        node[0][0]=node[0][1]=-1;
        re[0]=0;
        for(int i=1; i<=n; ++i)
            scanf("%d",&a[i]),update(a[i],1);
        int ans=0;
        for(int i=1; i<n; ++i)
        {
            update(a[i],-1);
            for(int j=i+1; j<=n; ++j)
            {
                update(a[j],-1);
                ans=max(ans,getans(a[i]+a[j]));
                update(a[j],1);
            }
            update(a[i],1);
        }
        printf("%d\n",ans);
    }
    return 0;
}

时间: 2024-08-09 23:54:38

HDU 5536 Chip Factory 字典树+贪心的相关文章

hdu 5536 Chip Factory 字典树+bitset 铜牌题

Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1842    Accepted Submission(s): 833 Problem Description John is a manager of a CPU chip factory, the factory produces lots of chip

ACM学习历程—HDU 5536 Chip Factory(xor &amp;&amp; 字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. 此外题目中说大数据只有10组,小数据最多n只有100.(那么c*n^2的复杂度应该差不多) 于是可以考虑枚举i和j,然后匹配k. 于是可以先把所有s[k]全部存进一个字典树, 然后枚举s[i]和s[j],由于i.j.k互不相等,于是先从字典树里面删掉s[i]和s[j],然后对s[i]+s[j]这个

HDU 5536 Chip Factory(字典树)

题目戳这 题意:给你n个数,从中选出三个数a,b,c,然后(a+b)^c,这个式子的值是最大的. 思路:如果来三个for循环,果断TLE,所以想到了字典树,就是先把所有的数字都插进去,在查询的时候就先把确定了的那两个数字删掉,然后在字典树中求出异或的最大值,得到结果后,再把两个数字插回去. P.S.一开始我的数组开到了45*10^5+10,然后他给我TLE,然后当我把数组开到10^5+10的时候,A了···········,居然是数组爆我时间了,我勒个擦. #include<stdio.h>

2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory

Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 368    Accepted Submission(s): 202 Problem Description John is a manager of a CPU chip factory, the factory produces lots of chips

HDU 5536: Chip Factory

import static java.lang.Math.*;import java.util.*; /** * @author Sycamore * @link http://acm.hdu.edu.cn/showproblem.php?pid=5536 * @date Sep, 16 */ public class Main { public static void main(String args[]) { Scanner scanner =new Scanner(System.in);

01字典树贪心查询+建立+删除(个人模版)

01字典树贪心查询+建立+删除: 1 #define maxn 2 2 typedef struct tree 3 { 4 tree *nex[maxn]; 5 int v; 6 int val; 7 }tree; 8 tree root; 9 void init() 10 { 11 for(int i=0;i<maxn;i++) 12 { 13 root.nex[i]=NULL; 14 } 15 } 16 void creat(char *str,int va) 17 { 18 int len

HDU 1075 map or 字典树

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4069 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

HDU 4825 Xor Sum 字典树+位运算

点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 151 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus

hdu 1251 统计难题 字典树

// hdu 1251 统计难题 字典树 // // 题目大意: // // 有一系列的单词表,以空行结尾,之后会有一些字母串,找出以这些字符串 // 作为前缀的单词的个数 // // // 解题思路: // // 字典树 Trie,在插入字符串的时候每遇到一个节点,该节点的值++.查找的时候 // 字符串时,如果找到了,那么返回当前的val,否则返回0,因为没有以这个字符串 // 为前缀的单词. // // // 感悟: // // 这段时间想学学数据结构,就看了看刘老的大白书,感觉用数组挺巧