ZOJ 3870 Team Formation

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ 3870 Team Formation相关的知识,希望对你有一定的参考价值。

2015浙江省赛B题。我用了枚举+二分。。时间复杂度o(64*n),1900ms跑过的。应该有更好的方法。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;

const int maxn = 40;
int base[maxn], tot;
int T, n;
int a[100000 + 10];
int tmp, l, r, mid;
long long ans;

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
        sort(a + 1, a + 1 + n);
        ans = 0;
        for (int i = 1; i <= n; i++)
        {
            memset(base, 0, sizeof base);
            tot = 0; tmp = a[i];
            while (tmp) base[tot++] = tmp % 2, tmp = tmp / 2;

            for (int j = tot - 1; j >= 0; j--)
            {
                if (base[j] == 0)
                {
                    int MIN = (1 << j);
                    int MAX = (1 << (j + 1)) - 1;

                    int pos1 = -1, pos2 = -1;

                    l = 1, r = n, mid;
                    while (l <= r)
                    {
                        mid = (l + r) / 2;
                        if (a[mid] >= MIN) r = mid - 1, pos1 = mid;
                        else l = mid + 1;
                    }

                    l = 1, r = n, mid;
                    while (l <= r)
                    {
                        mid = (l + r) / 2;
                        if (a[mid] <= MAX) l = mid + 1, pos2 = mid;
                        else r = mid - 1;
                    }

                    if (pos1 != -1 && pos2 != -1) ans = ans + (long long)(pos2 - pos1 + 1);
                }
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

以上是关于ZOJ 3870 Team Formation的主要内容,如果未能解决你的问题,请参考以下文章

zoj 3870

Team Formation

[虚拟机OA]Team Formation 2 团队构成

TZOJ--4986: Team Formation(二进制枚举)

hdu 3870 最小割转化为最短路径2

XJOI 3870 游戏的期望