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 chips everyday. To manage large amounts of products, every processor has a serial number. More specifically, the factory produces  chips today, the -th chip produced this day has a serial number .

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:

which  are three different integers between  and . 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  indicating the total number of test cases.

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




There are at most  testcases with 

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个整型数(n<=1e3),现在可以从其中选出s1,s2,s3三个数字,要求求出(s1+s2)^s3的最大值,至多

1e3组测试数据。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf
const int N=1e3+10;

int ch[40*N][3];
int a[N],sz,num[40*N],val[40*N];
bitset<32> v;

void add(int k)
{
   int p=0;
   for(int i=30;i>=0;i--){
      int id=v[i];
      if(ch[p][id]==-1)  ch[p][id]=++sz;
      p=ch[p][id];
      num[p]+=k;
   }
   if(k!=-1) val[p]=v.to_ullong();//返回bitset的值
}

int query()
{
   int p=0;
   for(int i=30;i>=0;i--){
      int id=v[i],ano=ch[p][!id];
      if(ano!=-1&&num[ano]!=0) p=ano;
      else p=ch[p][id];
   }
   return val[p];
}

int main()
{
    int cas,n;
    SC("%d",&cas);
    while(cas--){
        MM(ch,-1);MM(num,0);sz=0;
        SC("%d",&n);
        for(int i=1;i<=n;i++){
            SC("%d",&a[i]);
            v=a[i];
            add(1);
        }

        int ans=0;
        for(int i=1;i<=n;i++){
            v=a[i];add(-1);
            for(int j=i+1;j<=n;j++){
               v=a[j];add(-1);
               v=a[i]+a[j];
               ans=max(ans,(a[i]+a[j])^query());
               v=a[j];add(1);
            }
            v=a[i];add(1);
        }
        printf("%d\n",ans);
    }
    return 0;
}

 错因:刚开始想到的是枚举下s1+s2,然后,,插入s3,,,但是发现根本处理不了重合的情况,,,。。

解答:其实先依次添加元素建好字典树后,,可以n^2的枚举s1+s2,同时在字典树中抹去这两个元素,

这时再插入s3,,每次操作完成后都将s1,s2再添加进去。。

时间: 2024-10-05 10:20:26

hdu 5536 Chip Factory 字典树+bitset 铜牌题的相关文章

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&

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 1251 裸的字典树、入门题

裸的字典树还是挺简单的. 四个基本操作建立.查找.插入.删除 建立新结点我是用的c++中 new操作.当然也可以用malloc,都方便 不过指针阿.地址阿.这其中关系什么的我貌似还不是很清楚阿. 因为刚开始我的头结点也是定义的指针.然后程序就炸了.我不清楚原因呢. 有待弄清楚. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #includ

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 1247:Hat’s Words(字典树,经典题)

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7282    Accepted Submission(s): 2639 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly

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