hdu5269 Chip Factory

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5536

题目:

Chip Factory

Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2044    Accepted Submission(s): 919

Problem Description

John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage large amounts of products, every processor has a serial number. More specifically, the factory produces n chips today, the i-th chip produced this day has a serial number si.

At the end of the day, he packages all the chips produced this day, and send it to wholesalers. More specially, he writes a checksum number on the package, this checksum is defined as below:

maxi,j,k(si+sj)⊕sk

which i,j,k are three different integers between 1 and n. And ⊕ is symbol of bitwise XOR.

Can you help John calculate the checksum number of today?

Input

The first line of input contains an integer T indicating the total number of test cases.

The first line of each test case is an integer n, indicating the number of chips produced today. The next line has n integers s1,s2,..,sn, separated with single space, indicating serial number of each chip.

1≤T≤1000
3≤n≤1000
0≤si≤109
There are at most 10 testcases with n>100

Output

For each test case, please output an integer indicating the checksum number in a line.

Sample Input

2

3

1 2 3

3

100 200 300

Sample Output

6

400

思路:这题居然可以N^3爆过去,我也是惊呆了==,数据太水了吧。正解是01trie。

暴力代码:

#include<stdio.h>
int a[1001];
int max(int ta,int b)
{
    if(ta>b)
        return ta;
    return b;
}
int sc(int ta,int tb,int tc,int ans)
{
    return max(ans,(ta+tb)^tc);
}
int main(void)
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        int ans=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
            for(int k=j+1;k<=n;k++)
            ans=sc(a[i],a[j],a[k],ans),ans=sc(a[j],a[k],a[i],ans),ans=sc(a[i],a[k],a[j],ans);
        printf("%d\n",ans);
    }
    return 0;
}

01trie代码:

#include <stdio.h>
#include <string.h>
struct Trie
{
    int root, tot, next[100001][2], cnt[100001], end[100001];

    inline int Newnode()
    {
        memset(next[tot], -1, sizeof(next[tot]));
        cnt[tot] = 0;
        end[tot] = 0;
        return tot ++;
    }

    inline void Init()
    {
        tot = 0;
        root = Newnode();
    }

    inline void Insert(int x)
    {
        int p = root;
        cnt[p] ++;
        for(int i = 31; i >= 0; i --)
        {
            int idx = ((1 << i) & x) ? 1 : 0;
            if(next[p][idx] == -1)
                next[p][idx] = Newnode();
            p = next[p][idx];
            cnt[p] ++;
        }
        end[p] = x;
    }

    inline void Del(int x)
    {
        int p = root;
        cnt[p] --;
        for(int i = 31; i >= 0; i --)
        {
            int idx = ((1 << i) & x) ? 1 : 0;
            p = next[p][idx];
            cnt[p] --;
        }
    }

    inline int Search(int x)
    {
        int p = root;
        for(int i = 31; i >= 0; i --)
        {
            int idx = ((1 << i) & x) ? 1 : 0;
            if(idx == 0)
            {
                if(next[p][1] != -1 && cnt[next[p][1]])
                    p = next[p][1];
                else
                    p = next[p][0];
            }
            else
            {
                if(next[p][0] != -1 && cnt[next[p][0]])
                    p = next[p][0];
                else
                    p = next[p][1];
            }
        }
        return x ^ end[p];
    }
}tr;
int max(int ta,int tb)
{
    return ta>=tb?ta:tb;
}
int a[1001];
int main(void)
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,ans=0;scanf("%d",&n);
        tr.Init();
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]),tr.Insert(a[i]);
        for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        {
            tr.Del(a[i]),tr.Del(a[j]);
            ans=max(ans,tr.Search(a[i]+a[j]));
            tr.Insert(a[i]),tr.Insert(a[j]);
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-10 21:15:13

hdu5269 Chip Factory的相关文章

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 字典树+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

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&

hdu5536 Chip Factory xor,字典树

hdu5536 Chip Factory   xor,字典树 Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 280    Accepted Submission(s): 158 Problem Description John is a manager of a CPU chip factory, the

Chip Factory

原文链接:https://blog.csdn.net/qq_41021816/java/article/details/82934486 John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage large amounts of products, every processor has a serial number. More speci?cally, the

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);

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>

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】【 2015ACM/ICPC亚洲区长春站 】Chip Factory 题意&题解&代码

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意: 给定n个个数s1, s2- sn,求最大的(si+sj)^sk且满足(i!=j!=k). 题解: 很明显的一道字典树题目,把每个数都插入字典树,枚举两个数的和.考虑到可能会有重复的数,每次枚举到i,j时首先在字典树上删除 si 和 sj 然后再查询 si+sj . 代码: #include<iostream> #include<algorithm> #include&l