码迷,mamicode.com
首页 > 其他好文 > 详细

Chip Factory

时间:2020-04-02 22:37:16      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:--   tor   scan   tin   dex   思路   const   ons   proc   

原文链接: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 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 de?ned as below:

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?                                                   

输入

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≤s i≤109
There are at most 10 testcases with n > 100
输出

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

样例输入

2
3
1 2 3
3
100 200 300
 样例输出

6
400
                                                           

题目大意:
给 N 个数,在这 N 个数里找到三个值 i, j,k 使得(i+j)⊕ k 最大,输出这个最大值。

解题思路:
把每个数字看成一个0101字符串插入倒Trie树中去,枚举i和j,然后把si和sj从Trie树中删去。
然后在Trie树中贪心找到能与si+sj异或得到的最大值。
具体匹配的过程中是这样的,首先看树中最高位能否异或得到1。
能的话就往能的那个方向走,否则往另外一个方向走。

另外删除操作是这样实现的,我们每个节点记录一个num值。
插入时对所有经过节点的num值加1,删除就将对应节点的num值减1。
在树上匹配的时候就只走那些num值为正的节点。

#include <bits/stdc++.h>
#define ll long long int
#define INF 0x3f3f3f3f
using namespace std;
 
const int MAXN = 1e3+10;
int ch[MAXN*32][2];
ll value[MAXN*32];
ll b[MAXN];
int num[MAXN*32];
int node_cnt;
 
void init()
{
    memset(ch[0], 0, sizeof(ch[0]));
    node_cnt = 1;
}
 
void Insert(ll x)
{
    int cur = 0;
    for(int i = 32; i >= 0; i--){
        int index = (x>>i)&1;
        if(!ch[cur][index]){
            memset(ch[node_cnt], 0, sizeof(ch[node_cnt]));
            ch[cur][index] = node_cnt;
            value[node_cnt] = 0;
            num[node_cnt++] = 0;
        }
        cur = ch[cur][index];
        num[cur]++;
    }
    value[cur] = x;
}
 
void update(ll x, int d)
{
    int cur = 0;
    for(int i =32; i >= 0; i--){
        int index = (x>>i)&1;
        cur = ch[cur][index];
        num[cur]+=d;
    }
}
 
ll query(ll x)
{
    int cur = 0;
    for(int i = 32; i >= 0; i--)
    {
        int index = (x>>i)&1;
        if(ch[cur][index^1] && num[ch[cur][index^1]]) cur = ch[cur][index^1];
        else cur = ch[cur][index];
    }
    return x^value[cur];
}
 
int main()
{
    int T_case, N;
    scanf("%d", &T_case);
    while(T_case--)
    {
        scanf("%d", &N);
        init();
        for(int i = 1; i <= N; i++)
        {
            scanf("%lld", &b[i]);
            Insert(b[i]);
        }
        ll ans = 0;
        for(int i = 1; i <= N; i++){
            for(int j = 1; j <= N; j++){
                if(i == j) continue;
                update(b[i], -1);
                update(b[j], -1);
                ans = max(ans, query(b[i]+b[j]));
                update(b[i], 1);
                update(b[j], 1);
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

  然而。。。。

#include <cstdio>
#include <algorithm>
using namespace std;
int a[1005];

int main()
{
	int T;
	scanf("%d", &T);
	while(T --)
	{
		int n, ma = 0;
		scanf("%d" ,&n);
		for(int i = 0; i < n; i++)
			scanf("%d", &a[i]);
		for(int i = 0; i < n; i++)
			for(int j = i + 1; j < n; j++)
				for(int k = j + 1; k < n; k++)
				{
					ma = max(ma, (a[i] + a[j]) ^ a[k]);
					ma = max(ma, (a[i] + a[k]) ^ a[j]);
					ma = max(ma, (a[j] + a[k]) ^ a[i]);
				}
		printf("%d\n", ma);
	}
}

  

Chip Factory

标签:--   tor   scan   tin   dex   思路   const   ons   proc   

原文地址:https://www.cnblogs.com/cutemush/p/12623323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!