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

ZOJ 3870 Team Formation

时间:2016-02-22 20:45:26      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/5207970.html

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