HDU 5536 Chip Factory 01trie

题目:

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

题意:

给出 n 个数,从这些数字中任选3个不同的数字 x,y,z ,求出一个值 (x+y)z ,求所有值里面的最大值

思路:

暴力居然可过。。。然后正解是用字典树去求最大异或值,首先把 n 个数字插入到01trie中,然后枚举 x+y ,首先从字典树中删掉 x,y ,然后去查询与 x+y 异或能得出的最大值,最后取这些值中的最大值

#include <bits/stdc++.h>

using namespace std;

const int N = 1000 + 10, M = 2, INF = 0x3f3f3f3f;

struct node
{
    node *next[M];
    int val;
    void init()
    {
        memset(next, 0, sizeof next);
        val = 0;
    }
}trie[N*32], *root;
int tot;
int len = 31;

node *new_node()
{
    trie[tot].init();
    return trie + tot++;
}
void trie_init()
{
    tot = 0;
    root = new_node();
}
void trie_insert(int x)
{
    node *p = root;
    for(int i = len-1; i >= 0; i--)
    {
        int j = 1 & (x>>i);
        if(p->next[j] == NULL) p->next[j] = new_node();
        p->next[j]->val++;
        p = p->next[j];
    }
}
int trie_query(int x)
{
    node *p = root;
    int ans = 0;
    for(int i = len-1; i >= 0; i--)
    {
        int j = ! (1 & (x>>i));
        if(p->next[j] != NULL && p->next[j]->val != 0)
        {
            p = p->next[j];
            ans = ans * 2 + 1;
        }
        else
        {
            p = p->next[!j];
            ans = ans * 2 + 0;
        }
    }
    return ans;
}
void trie_del(int x)
{
    node *p = root;
    for(int i = len-1; i >= 0; i--)
    {
        int j = 1 & (x>>i);
        p->next[j]->val--;
        p = p->next[j];
    }
}
int main()
{
    int t, n;
    int a[N];
    scanf("%d", &t);
    while(t--)
    {
        trie_init();
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            trie_insert(a[i]);
        }
        int ans = 0;
        for(int i = 1; i <= n; i++)
            for(int j = i+1; j <= n; j++)
            {
                trie_del(a[i]); trie_del(a[j]);
                ans = max(ans, trie_query(a[i] + a[j]));
                trie_insert(a[i]); trie_insert(a[j]);
            }
        printf("%d\n", ans);
    }
    return 0;
}

暴力代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 1000 + 10, INF = 0x3f3f3f3f;

int a[N];

int main()
{
    int t, n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        int ans = 0;
        for(int i = 1; i <= n; i++)
            for(int j = i+1; j <= n; j++)
                for(int k = j+1; k <= n; k++)
                {
                    ans = max(ans, (a[i] + a[j]) ^ a[k]);
                    ans = max(ans, (a[j] + a[k]) ^ a[i]);
                    ans = max(ans, (a[i] + a[k]) ^ a[j]);
                }
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值