zoukankan      html  css  js  c++  java
  • 牛客假日团队赛1 G.Superbull

    链接:

    https://ac.nowcoder.com/acm/contest/918/G

    题意:

    Bessie and her friends are playing hoofball in the annual Superbull championship, and Farmer John is in charge of making the tournament as exciting as possible. A total of N (1 <= N <= 2000) teams are playing in the Superbull. Each team is assigned a distinct integer team ID in the range 1...2^30-1 to distinguish it from the other teams. The Superbull is an elimination tournament -- after every game, Farmer John chooses which team to eliminate from the Superbull, and the eliminated team can no longer play in any more games. The Superbull ends when only one team remains.

    Farmer John notices a very unusual property about the scores in matches! In any game, the combined score of the two teams always ends up being the bitwise exclusive OR (XOR) of the two team IDs. For example, if teams 12 and 20 were to play, then 24 points would be scored in that game, since 01100 XOR 10100 = 11000.

    Farmer John believes that the more points are scored in a game, the more exciting the game is. Because of this, he wants to choose a series of games to be played such that the total number of points scored in the Superbull is maximized. Please help Farmer John organize the matches.

    思路:

    O(n^2)求出所有异或值,记录下标,再排序,从大到小选择,并查集维护规则即可。

    代码:

    #include <bits/stdc++.h>
     
    using namespace std;
     
    typedef long long LL;
    const int MAXN = 2e3 + 10;
    const int MOD = 1e9 + 7;
     
    struct Node
    {
        int v;
        int p1, p2;
        bool operator < (const Node & that) const
        {
            return this->v > that.v;
        }
    }node[MAXN*MAXN];
    int n, m, k, t;
    int a[MAXN];
    int Fa[MAXN];
    map<int, int> ti;
    map<int, int> li;
     
    int GetFa(int x)
    {
        if (Fa[x] == x)
            return x;
        Fa[x] = GetFa(Fa[x]);
        return Fa[x];
    }
     
    void Merge(int f1, int f2)
    {
        if (f1 > f2)
            swap(f1, f2);
        Fa[f1] = f2;
    }
     
    int main()
    {
        scanf("%d", &n);
        for (int i = 1;i <= n;i++)
            scanf("%d", &a[i]), li[a[i]] = i, Fa[i] = i;
        int cnt = 0;
        for (int i = 1;i <= n;i++)
        {
            for (int j = i + 1; j <= n; j++)
            {
                node[++cnt].v = a[i]^a[j];
                node[cnt].p1 = a[i];
                node[cnt].p2 = a[j];
            }
        }
        sort(node+1, node+1+cnt);
        LL res = 0;
        int pos = 0;
        for (int i = 1;i <= cnt;i++)
        {
            int f1 = GetFa(li[node[i].p1]);
            int f2 = GetFa(li[node[i].p2]);
            if (f1 != f2)
            {
                Merge(f1, f2);
                res += node[i].v;
                pos++;
            }
            if (pos == n-1)
                break;
        }
        cout << res << endl;
     
        return 0;
    }
    
  • 相关阅读:
    php无法保存cookies问题解决
    织梦(DEDECMS)首页调用相关投票的方法(自动更新)
    php导出任意mysql数据库中的表去excel文件
    学用.NET实现AutoCAD二次开发
    JS自动滚屏程序
    object c 的入门教程
    php如何截取字符串并以零补齐str_pad() 函数
    自己制作软键盘的几个关键技术解析
    php出现php_network_getaddresses的解决方法
    wamp环境下php命令运行时出现错误:无法启动此程序,因为计算机中丢失OCI.dll。尝试重新安装该程序以解决此问题
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10995670.html
Copyright © 2011-2022 走看看