【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<stdio.h>
#include<string.h>
using namespace std;
int n,m,tot,T,cnt[70005];
int tr[70005][2],s[1005];
void init(int i)
{
    tr[i][1]=tr[i][0]=0;
    cnt[i]=0;
}
void add(int x,int id)
{
    int now=0;
    for (int i=31;i>=0;i--)
    {
        int p=0;
        if (((1<<i)&x)!=0) p=1;
        if (!tr[now][p])
        {
            tot++;
            tr[now][p]=tot;
            init(tot);
        }
        now=tr[now][p];
        cnt[now]+=id;
    }
}
int query(int x)
{
    int now=0;
    int tmp=0;
    for (int i=31;i>=0;i--)
    {
        int p=1;
        if (((1<<i)&x)!=0) p=0;
        if (!tr[now][p] || cnt[tr[now][p]]<=0) now=tr[now][!p];
        else tmp+=(1<<i),now=tr[now][p];
    }
    return tmp;
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        tot=0;
        init(tot);
        scanf("%d",&n);
        for (int i=1;i<=n;i++)
        {
            scanf("%d",&s[i]);
            add(s[i],1);
        }
        int ans=-1;
        for (int i=1;i<=n;i++)
        for (int j=i+1;j<=n;j++)
        {
            add(s[i],-1);
            add(s[j],-1);
            ans=max(ans,query(s[i]+s[j]));
            add(s[i],1);
            add(s[j],1);
        }
        memset(cnt,0,sizeof(cnt));
        printf("%d\n",ans);
    }
}
时间: 2024-10-29 19:07:57

【hdu 5536】【 2015ACM/ICPC亚洲区长春站 】Chip Factory 题意&题解&代码的相关文章

HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array

Almost Sorted Array Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But sometimes it is an overkill to use these algorithms for an almost sorted arr

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

2015ACM/ICPC亚洲区长春站 G hdu 5534 Partial Tree

Partial Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 228    Accepted Submission(s): 138 Problem Description In mathematics, and more specifically in graph theory, a tree is an undirect

2015ACM/ICPC亚洲区长春站 L hdu 5538 House Building

House Building Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 145    Accepted Submission(s): 123 Problem Description Have you ever played the video game Minecraft? This game has been one of t

2015ACM/ICPC亚洲区长春站 B hdu 5528 Count a * b

Count a * b Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 211    Accepted Submission(s): 116 Problem Description Marry likes to count the number of ways to choose two non-negative integers a

2015ACM/ICPC亚洲区长春站 A hdu 5527 Too Rich

Too Rich Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 245    Accepted Submission(s): 76 Problem Description You are a rich person, and you think your wallet is too heavy and full now. So yo

2015ACM/ICPC亚洲区长春站 G hdu 5533 Dancing Stars on Me

Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 186    Accepted Submission(s): 124 Problem Description The sky was brushed clean by the wind and the stars were cold in a bl

2015ACM/ICPC亚洲区长春站 F hdu 5533 Almost Sorted Array

Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 447    Accepted Submission(s): 201 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort,

2015ACM/ICPC亚洲区长春站 E hdu 5531 Rebuild

Rebuild Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 446    Accepted Submission(s): 113 Problem Description Archaeologists find ruins of Ancient ACM Civilization, and they want to rebuild i

(HDU 5558) 2015ACM/ICPC亚洲区合肥站---Alice&#39;s Classified Message(后缀数组)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5558 Problem Description Alice wants to send a classified message to Bob. She tries to encrypt the message with her original encryption method. The message is a string S, which consists of Nlowercase let